[英]Nested resource view spec seems to be referring to a non-existent route
Rails 3.1.0 Rspec 2 Rails 3.1.0 Rspec 2
In a view spec for a nested resource, do I need to instantiate/stub the parent resource before I stub the nested resource? 在嵌套资源的视图规范中,在对嵌套资源进行存根处理之前,是否需要实例化/存根父资源?
I am asking this because all my view specs are failing for a new nested resource I have introduced in my application. 我之所以这样问,是因为我的所有视图规范都对我在应用程序中引入的新嵌套资源失败。 The nested resource works as expected when I manually test it though :( Here's how my edit view spec looks like.
嵌套的资源可以通过手动测试来达到预期的效果:(这是我的编辑视图规格的外观。
----- "./spec/views/sub_categories/edit.html.erb_spec.rb" - start --------
require 'spec_helper' describe "sub_categories/edit.html.erb" do before(:each) do
@sub_category = assign(:sub_category, stub_model(SubCategory,
:name => 'International interest rates',
:description => 'Comprehensive rates covering Australia, NZ, Malaysia and Singapore',
:category_id => 3,
:created_by => 1,
:updated_by => 1
)) end it "renders the edit sub category form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => category_sub_categories(@sub_category), :method => "post" do
assert_select "input#sub_category_name", :name => "sub_category[name]"
assert_select "textarea#sub_category_description", :name => "sub_category[description]"
end end end
--- "./spec/views/sub_categories/edit.html.erb_spec.rb" - end --------
Here's an extract of the failure: 以下是失败的摘要:
----------- extract start -------------------------
1) sub_categories/edit.html.erb renders the edit sub category form
Failure/Error: render
ActionView::Template::Error:
undefined method `sub_category_path' for #<#<Class: 0x0000010127d2b8>:0x000001016e2380>
# ./app/views/sub_categories/_form.html.erb:1:in `_app_views_sub_categories__form_html_erb__4092631658606598204_2155519360'
# ./app/views/sub_categories/edit.html.erb:3:in `_app_views_sub_categories_edit_html_erb___3853358586184509671_2155544160'
# ./spec/views/sub_categories/edit.html.erb_spec.rb:15:in `block (2 levels) in <top (required)>'
----------- extract end -------------------------
Here's what my form partial looks like 这是我的部分表格的样子
----- app/views/sub_categories/_form.html.erb start ---------------------
<%= form_for [@category, @sub_category] do |f| %> <% if @sub_category.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@sub_category.errors.count, "error") %> prohibited this sub_category from being saved:</h2>
<ul>
<% @sub_category.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div> <% end %> <div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %> </div> <div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %> </div> <div class="actions">
<%= f.submit %> </div> <% end %>
----- app/views/sub_categories/_form.html.erb end ---------------------
Here's what I see when I run 'rake routes': 这是我运行“耙路”时看到的内容:
------- routes start --------------------------- category_sub_categories GET /categories/:category_id/ sub_categories(.:format) {:action=>"index", :controller=>"sub_categories"}
POST /categories/:category_id/ sub_categories(.:format) {:action=>"create", :controller=>"sub_categories"} new_category_sub_category GET /categories/:category_id/ sub_categories/new(.:format) {:action=>"new", :controller=>"sub_categories"} edit_category_sub_category GET /categories/:category_id/ sub_categories/:id/edit(.:format) {:action=>"edit", :controller=>"sub_categories"}
category_sub_category GET /categories/:category_id/ sub_categories/:id(.:format) {:action=>"show", :controller=>"sub_categories"}
PUT /categories/:category_id/ sub_categories/:id(.:format) {:action=>"update", :controller=>"sub_categories"}
DELETE /categories/:category_id/ sub_categories/:id(.:format) {:action=>"destroy", :controller=>"sub_categories"}
categories GET / categories(.:format) {:action=>"index", :controller=>"categories"}
POST / categories(.:format) {:action=>"create", :controller=>"categories"}
new_category GET /categories/ new(.:format) {:action=>"new", :controller=>"categories"}
edit_category GET /categories/:id/ edit(.:format) {:action=>"edit", :controller=>"categories"}
category GET / categories/:id(.:format) {:action=>"show", :controller=>"categories"}
PUT / categories/:id(.:format) {:action=>"update", :controller=>"categories"}
DELETE / categories/:id(.:format) {:action=>"destroy", :controller=>"categories"}
root /
------- routes end ---------------------------
The form partial has been properly fitted with the parent resource and the nested resource (ie. " form_for [@category, @sub_category]
" ). 部分表单已经正确地适合了父资源和嵌套资源(即“
form_for [@category, @sub_category]
”)。 It seems that it's calling a route, sub_category_path which I have never specified. 似乎它正在调用一条我从未指定过的路由sub_category_path。
The error comes up when an edit/create form is to be created where a form partial is called. 当要在其中调用表单局部的地方创建编辑/创建表单时出现错误。
I am really puzzled on why this is happening and have consulted the search results I had via google for 'nested resources with rspec' , 'Rails in Action 3' by Yehuda Katz, and the Rspec book :( 我真的对为什么会发生这种情况感到疑惑,并查阅了我在Google上搜索过的搜索结果,其中包括“带有rspec的嵌套资源”,Yehuda Katz的“ Rails in Action 3”和Rspec书:(
If anyone knows what I am missing, I would love to hear your thoughts. 如果有人知道我在想什么,我很想听听您的想法。
Your category_id
is in @sub_category.category_id
not in @category
which you use in the view (which is nil now). 您的
category_id
位于@sub_category.category_id
而不位于您在视图中使用的@category
中(现在为nil)。
[@category, @sub_category]
is just a shortcut for url_for([@category, @sub_category])
which returns single resource path if one the arguments is nil. [@category, @sub_category]
url_for([@category, @sub_category])
[@category, @sub_category]
只是url_for([@category, @sub_category])
的快捷方式,如果其中一个参数为nil,则返回单个资源路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.