Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 806
GSto Avatar asked Nov 06 '10 00:11

GSto


1 Answers

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 
like image 81
Zabba Avatar answered Oct 21 '22 07:10

Zabba