Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails路由:覆盖资源/成员块中的操作名称_Ruby On Rails_Routes_Rspec2 - Fatal编程技术网

Ruby on rails Rails路由:覆盖资源/成员块中的操作名称

Ruby on rails Rails路由:覆盖资源/成员块中的操作名称,ruby-on-rails,routes,rspec2,Ruby On Rails,Routes,Rspec2,我知道如何使用match执行此操作,但我确实希望在资源块中执行此操作。以下是我所做的(简化): 我正在寻找一种方法,允许在URL中使用相同的操作名称,但在控制器中使用不同的入口点。目前我的控制器中有: # PUT /stories/1/collaborator.json def add_collaborator ... end # DELETE /stories/1/collaborator.json def remove_collaborator ... end 所以我试了一下:

我知道如何使用
match
执行此操作,但我确实希望在资源块中执行此操作。以下是我所做的(简化):

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

# 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文件生成了哪些路由。

这使路由在rake routes输出中正确显示,对此我永远心存感激:)我仍然遇到rspec故障,但现在至少我知道不是我的路由被破坏了。谢谢不客气,也许我可以帮你进一步解决rspec失败的问题?那太棒了。我试着用get路由做一个简单得多的例子,并用:path命名。从浏览器上看,它完全可以工作。当我做
xhr:get,:test\u path时,
它找不到路由,基本信息与我原来的帖子中的相同。在进一步的探索中,如果我在rspec测试中使用实际操作名称,一切都会起作用:
xhr:put,:add\u collaborator,
。我在想,我应该在路由测试中测试路由映射是否有效,然后让它继续:)在stories\u controller\u spec.rb文件的第78行附近有什么内容?这是你在帖子中已经展示的吗?我认为这个错误与rspec任务有关,但不确定。如果能奏效那就太好了。如果您需要进一步帮助,请告诉我,我会尽力帮助您。:)
$ rake routes > routes.txt