gpt4 book ai didi

ruby-on-rails - rails : Add Custom action to resource

转载 作者:数据小太阳 更新时间:2023-10-29 06:36:24 26 4
gpt4 key购买 nike

我有一个故事 Controller ,我已将其映射为资源。我向 stories_controller 添加了 2 个新方法,'top' 和 'latest'。但是当我尝试访问 example.com/stories/top 时,出现“没有 ID=top 的故事”错误。如何更改路由以识别这些 URL?

最佳答案

在 Rails 2.x 中尝试:

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

在 Rails 3.x 中:

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

关于ruby-on-rails - rails : Add Custom action to resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4111271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com