Rails 3功能测试 - 'get()'无法访问控制器方法
问题描述:
在我的项目中,故事属于用户,通常所有故事的访问都是通过嵌套资源访问,除了显示全局列表所有的故事。在我的routes.rb文件我有:Rails 3功能测试 - 'get()'无法访问控制器方法
resources :users do
resources :stories
end
match "/all_stories" => "stories#all_stories"
的match
语句生成以下路线:
all_stories /all_stories(.:format) {:controller=>"stories", :action=>"all_stories"}
在我stories_controller.rb
文件我有一个名为动作:all_stories
。
def all_stories
@stories = Story.all
end
从我的观点,我以这种方式调用link_to
:
<%= link_to "All Stories", all_stories_path %>
造成此错误:
No route matches {:action=>"show", :controller=>"stories", :user_id=>nil,
:id=>#<Story id: 1, title: "test story 01", desc: "Edited desc for test story 01",
activity: 20, created_at: "2011-11-28 01:07:08", updated_at: "2011-11-28 01:26:31",
user_id: 5>}
在我stories_controller_test.rb
以下GET呼吁:all_stories
生成上市错误:
get('all_stories')
Undefined method `story_path' for #<#<Class:0x0000010430da18>:0x00000101789d10>
get(:all_stories)
undefined method `story_path' for #<#<Class:0x00000102a178b0>:0x00000100ee9cc8>
get('/all_stories')
No route matches {:controller=>"stories", :action=>"/all_stories"}
get({:action=>"all_stories"})
No route matches {:controller=>"stories", :action=>"{:action=>\"all_stories\"}"}
在前两个,没有'story_path“,因为故事嵌套在用户下。不知道接下来两个会发生什么。在我的控制器中,我可以使用什么语法来get
all_stories
操作?
答
尝试改变路线
match "/all_stories" => "stories#all_stories", :as => :all_stories
然后在你的代码,你可以参考all_stories_path
或all_stories_url
答
我有这个问题,但我能够使用@Controller 使用来解决它@控制
所罗门,感谢您的建议,但它没有奏效。我做了你的改变,并设置我的link_to/get方法引用:all_stories_path。我的link_to语句(在视图中)出错:没有路由匹配{:action =>“show”,:controller =>“stories”,:user_id => nil,:id =>#}我的功能测试错误与:无路线匹配{:controller =>”stories“,:action =>”/ all_stories“} –
尝试将您的匹配所有故事线放在资源部分上方。如果这不起作用,你的link_to语句是什么(也许有错字)?最后,为什么你不添加你的控制器代码,因为也许里面有东西...... – Solomon
我在你的评论中再次看到错误,它似乎指向link_to中的问题。在all_stories_path中,您不应该将故事作为:id参数传递。可能这不是all_stories路径的问题,就像您为每个单独故事显示的行中的问题一样。尽量不要在页面上显示任何信息以检查页面是否正确路由。 – Solomon