gpt4 book ai didi

ruby-on-rails - Rails 路由 : override the action name in a resource/member block

转载 作者:行者123 更新时间:2023-12-05 05:28:27 26 4
gpt4 key购买 nike

我知道如何用 match 做到这一点,但我真的想在资源 block 中做到这一点。这是我所拥有的(已简化):

resources :stories do
member do
'put' collaborator
'delete' collaborator
end
end

我正在寻找一种允许在 URL 中使用相同操作名称但在 Controller 中具有不同入口点的方法。目前我已经在我的 Controller 中:

# PUT /stories/1/collaborator.json
def add_collaborator
...
end

# DELETE /stories/1/collaborator.json
def remove_collaborator
...
end

所以我尝试了这个:

resources :stories do
member do
'put' collaborator, :action => 'add_collaborator'
'delete' collaborator, :action => 'remove_collaborator'
end
end

但是当我编写 rspec 单元测试时这似乎不起作用:

describe "PUT /stories/1/collaborator.json" do
it "adds a collaborator to the story" do
story = FactoryGirl.create :story
collaborator = FactoryGirl.create :user

xhr :put, :collaborator, :id => story.id, :collaborator_id => collaborator.id

# shoulds go here...
end

结束

我收到这个错误:

Finished in 0.23594 seconds
5 examples, 1 failure, 1 pending

Failed examples:

rspec ./spec/controllers/stories_controller_spec.rb:78 # StoriesController PUT
/stories/1/collaborator.json adds a collaborator to the story

我假设此错误是因为我尝试定义路线的方式不正确...有什么建议吗?

最佳答案

下面那个更好吗?

resources :stories do
member do
put 'collaborator' => 'controller_name#add_collaborator'
delete 'collaborator' => 'controller_name#remove_collaborator'
end
end

您还应该通过在终端中启动来检查您的路线:

$ rake routes > routes.txt

然后打开生成的 routes.txt 文件以查看从 routes.rb 文件生成了哪些路由。

关于ruby-on-rails - Rails 路由 : override the action name in a resource/member block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10353776/

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