Search code examples
ruby-on-rails-3controllercustom-routes

Rails3 custom controller method not routed: works in Rails2


I've been using Rails 2.3.8 for a project that I've been working on, but have just migrated the project over to Rails 3.0.3. After ironing out the basic bugs, I've got a problem now with a custom controller method.

In the gallery_controller, I had a custom method called 'extract'. In rails 2.3.8, this worked fine, no extra configuration. I could go to /galleries/extract/:id and it would do what I wanted it to.

Now that code breaks the app when I try to create a link to it with the original code in the form:

<%= link_to "Add photos to gallery from: ",
        :action => 'extract', :id => @gallery.id %>

and the error I get when I try to go to the page with this code on it:

No route matches {:action=>"extract", :controller=>"galleries", :id=>2}

After looking at routes.rb, I've come to suspect that this fails because the

match ':controller(/:action(/:id(.:format)))'

is not included, and is in fact deprecated.

Running rake tasks | grep 'extract' gives me nothing.

So how should I fix this?


Solution

  • See the Routing Guide: Adding More RESTful Actions. You need to tell your application's routes about the extract action, with something like this:

    resources :gallery do
      get 'extract', :on => :member
    end