Rails 3.1.10 中脚手架生成的视图规范中缺少部分错误

Posted

技术标签:

【中文标题】Rails 3.1.10 中脚手架生成的视图规范中缺少部分错误【英文标题】:Missing Partial error in view spec generated by scaffold in rails 3.1.10 【发布时间】:2013-02-06 20:32:48 【问题描述】:

我刚刚在 rails 3.1.10 中生成了一个脚手架,当我运行 rspec 时出现以下错误:

Failures:   1) organizations/edit.html.erb renders the edit organization form
     Failure/Error: render
     Missing partial /form with :handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]. Searched in:
       * "/home/ubuntu/Documents/github/LocalSupport/app/views"
     # ./app/views/organizations/edit.html.erb:3:in `_app_views_organizations_edit_html_erb__643434798_103899540'
     # ./spec/views/organizations/edit.html.erb_spec.rb:18:in `block (2 levels) in <top (required)>'

  2) organizations/new.html.erb renders new organization form
     Failure/Error: render
     Missing partial /form with :handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]. Searched in:
       * "/home/ubuntu/Documents/github/LocalSupport/app/views"
     # ./app/views/organizations/new.html.erb:3:in `_app_views_organizations_new_html_erb__277486420_91000650'
     # ./spec/views/organizations/new.html.erb_spec.rb:17:in `block (2 levels) in <top (required)>'

Finished in 0.68276 seconds 29 examples, 2 failures, 2 pending

似乎规范在运行渲染时找不到表单部分。这是 rails 生成规范中的失败部分(当它到达“render”关键字时失败):

require 'spec_helper'

describe "organizations/index.html.erb" do
  before(:each) do
    assign(:organizations, [
      stub_model(Organization,
        :name => "Name",
        :address => "Address",
        :postcode => "Postcode",
        :email => "Email",
        :description => "Description",
        :website => "",
        :telephone => "Telephone"
      ),
      stub_model(Organization,
        :name => "Name",
        :address => "Address",
        :postcode => "Postcode",
        :email => "Email",
        :description => "Description",
        :website => "",
        :telephone => "Telephone"
      )
    ])
  end

  it "renders a list of organizations" do
    render
    # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
    assert_select "tr>td", :text => "Name".to_s, :count => 2

我在谷歌上搜索过这个,但找不到任何特定于这个脚手架的东西。我发现了这个 SO 帖子:

Rails: "missing partial" when calling 'render' in RSpec test

但它似乎表明渲染是正确的方法,即脚手架是正确的。有任何想法吗?

在此先感谢

【问题讨论】:

【参考方案1】:

文件“./app/views/organizations/edit.html”在第 3 行有一个调用,可能读到类似

render "form" 可以等同于 => "organizations/_form.html.erb"

此文件丢失....

您可以提供一个或在 edit.html(.erb/.haml) 中编辑该行

【讨论】:

该文件存在:saasbook@saasbook:~/Documents/github/LocalSupport$ ls app/views/organizations/edit.html.erb _form.html.erb index.html.erb new.html .erb show.html.erb - 问题是它在错误的地方搜索 - 仔细查看错误消息 - 你会看到它说“搜索:”/home/ubuntu/Documents/github/LocalSupport/app/ views"' 它没有在 /home/ubuntu/Documents/github/LocalSupport/app/views/organizations 中寻找它应该在的位置 - 我正在尝试弄清楚如何解决这个问题......【参考方案2】:

我至少找到了一个临时解决方案,即在渲染操作之前将以下内容添加到规范中:

view.lookup_context.prefixes = %w[组织应用程序]

基于此处记录的 Rails 问题:

https://github.com/rails/rails/issues/5213

【讨论】:

以上是关于Rails 3.1.10 中脚手架生成的视图规范中缺少部分错误的主要内容,如果未能解决你的问题,请参考以下文章

Scaffold Rails 3 视图中的外键下拉列表

仅在 Rails 中搭建视图文件。可能的?

如何避免 Rails 脚手架将模型放入命名空间

Rails:需要帮助在另一个模型的视图中显示一个模型的表单

在 Rails 中更改脚手架控制器生成器模板

现有模型和数据库表的 rails g 脚手架