Rails:向资源添加自定义操作

GSt*_*Sto 15 ruby routes ruby-on-rails

我有一个故事控制器,我已将其映射为资源.我已经为stories_controller,'top'和'latest'添加了2个新方法.但是当我尝试去example.com/stories/top时,我得到了一个'没有ID = top'的故事.如何更改路由以识别这些网址?

Zab*_*bba 33

试试Rails 2.x:

map.resources :stories, :collection => { :top => :get , :latest => :get } 
Run Code Online (Sandbox Code Playgroud)

在Rails 3.x中:

resources :stories do 
  collection do 
    get 'top'
    get 'latest'
  end 
end 
Run Code Online (Sandbox Code Playgroud)