GSto
GSto

Reputation: 42380

Rails: Add Custom action to resource

I have a stories controller that i have mapped as a resource. I've added 2 new methods to stories_controller, 'top' and 'latest'. but when i try to go to example.com/stories/top I get a 'no story with ID=top' error. How can I change the routing to recognize these urls?

Upvotes: 15

Views: 8905

Answers (1)

Zabba
Zabba

Reputation: 65517

Try in Rails 2.x:

map.resources :stories, :collection => { :top => :get , :latest => :get } 

In Rails 3.x:

resources :stories do 
  collection do 
    get 'top'
    get 'latest'
  end 
end 

Upvotes: 33

Related Questions