【问题标题】:Nested resource view spec seems to be referring to a non-existent route嵌套资源视图规范似乎指的是不存在的路线
【发布时间】:2012-01-25 12:42:18
【问题描述】:

导轨 3.1.0 Rspec 2

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

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

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

表单部分已与父资源正确匹配 和嵌套资源(即 "form_for [@category, @sub_category]" )。 似乎它正在调用我拥有的路线 sub_category_path 从未指定。

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

我真的很困惑为什么会发生这种情况,并咨询了 我通过 google 获得的“带有 rspec 的嵌套资源”的搜索结果, Yehuda Katz 的“Rails in Action 3”和 Rspec 书 :(

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

【问题讨论】:

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


    【解决方案1】:

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

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

    检查guide

    【讨论】:

    • 谢谢你,吉比尔。你是对的!我使用存根 @category 对象更改了要赋予 category_sub_categories() 路径的对象,该错误消失了。
    • 不幸的是,出现了另一个错误。失败/错误:渲染 ActionView::Template::Error: 没有路由匹配 {:controller=>"sub_categories"} # ./app/views/sub_categories/edit.html.erb:6:in _app_views_sub_categories_edit_html_erb___1129420093949643160_2199243480' # ./spec/views/sub_categories/edit.html.erb_spec.rb:21:in block (2 个级别) in ' 我知道我的控制器在 app/controllers/sub_categories_controller.rb 中被定义为“SubCategoriesController”。我得调查一下。也许是路线文件。稍后会报告:) 谢谢:)
    • 在您的./spec/views/sub_categories/edit.html.erb_spec.rb 中。简短的回答是是的,在这种情况下你必须存根父资源。
    • 你好,jibiel,我查看了我的路线文件,一切似乎都井井有条。 ` 资源 :categories 做资源 :sub_categories end`
    • ActionView::Template::Error: No route matches {:controller=&gt;"sub_categories"} controller 没有问题。它说您没有指定action,因此rails 无法匹配任何已定义的路线。将您的edit.html.erb_spec.rbedit.html.erbroutes.rb 放到pastebin.com 中,以便我看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多