嵌套资源视图规范似乎指的是不存在的路由

时间:2012-01-25 12:42:18

标签: ruby-on-rails ruby-on-rails-3 rspec rspec2 nested-resources

Rails 3.1.0  Rspec 2

在嵌套资源的视图规范中,我是否需要实例化/ stub 存根嵌套资源之前的父资源?

我问这个是因为我的所有观看规范都失败了 我在我的应用程序中介绍的嵌套资源。嵌套 资源按预期工作 当我手动测试它虽然:(  这是我的编辑视图规范的样子。

----- "./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 --------

以下是失败的摘录:

----------- 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 -------------------------

这是我的表格部分看起来像

----- 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 ---------------------

这是我在运行'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 ---------------------------

表单partial已正确配置父资源 和嵌套资源(即“form_for [@category, @sub_category]”)。  它似乎正在调用我拥有的路径sub_category_path 从未指定。

当要创建编辑/创建表单时,会出现错误 形式部分被称为。

我真的很困惑为什么会这样,并咨询过 搜索结果我通过谷歌搜索'嵌套资源与rspec', Yehuda Katz的“Rails in Action 3”和Rspec书:(

如果有人知道我错过了什么,我很想听听你的想法。

1 个答案:

答案 0 :(得分:1)

category_id位于@sub_category.category_id,而不在@category中,您在视图中使用(现在为零)。

[@category, @sub_category]只是url_for([@category, @sub_category])的一个快捷方式,如果参数为零,则返回单个资源路径。

检查guide