0

我正在尝试构建一个简单的产品积压应用程序来自学 Rails。对于每个产品,可以有多个产品积压条目,所以我想创建一个产品视图,显示产品信息、产品的所有积压条目,并包括一个用于添加更多积压条目的嵌套表单。

一切正常,直到我尝试将表单添加到视图中,然后导致以下错误:

NoMethodError in Products#show

Showing app/views/products/show.html.erb where line #29 raised:

undefined method `pblog_ref' for #<Product:0x10423ba68>
Extracted source (around line #29):

26:   <%= f.error_messages %>
27:   <p>
28:     <%= f.label :pblog_ref %><br />
29:     <%= f.text_field :pblog_ref %>
30:   </p>
31:   <p>
32:     <%= f.label :product %><br />

报告问题的产品视图如下(部分工作正常,因此我不会包含该代码):

<h1>Showing product</h1>

<p>
  <b>Product ref:</b>
  <%=h @product.product_ref %>
</p>
<p>
  <b>Description:</b>
  <%=h @product.description %>
</p>
<p>
  <b>Owner:</b>
  <%=h @product.owner %>
</p>
<p>
  <b>Status:</b>
  <%=h @product.status %>
</p>

<h2>Product backlog</h2>
<div id="product-backlog">
  <%= render :partial => @product.product_backlogs %>
</div>

<% form_for(@product, ProductBacklog.new) do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :pblog_ref %><br />
    <%= f.text_field :pblog_ref %>
  </p>
  <p>
    <%= f.label :product %><br />
    <%= f.text_field :product %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </p>
  <p>
    <%= f.label :owner %><br />
    <%= f.text_field :owner %>
  </p>
  <p>
    <%= f.label :status %><br />
    <%= f.text_field :status %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>

这是产品型号:

class Product < ActiveRecord::Base
  validates_presence_of :product_ref, :description, :owner
  has_many :product_backlogs
end

这是 ProductBacklog 模型:

class ProductBacklog < ActiveRecord::Base
  belongs_to :product
end

这些是路线:

  map.resources :product_backlogs
  map.resources :products, :has_many => :product_backlogs

此模型的架构中存在所有列:

create_table "product_backlogs", :force => true do |t|
  t.string   "pblog_ref"
  t.integer  "product_id"
  t.string   "description"
  t.string   "owner"
  t.string   "status"
  t.datetime "created_at"
  t.datetime "updated_at"
end

我已经检查了我在用 Rails 2 截屏视频在 15 分钟内创建一个博客,原则上我似乎在做和他一样的事情——只有他的嵌套评论表单有效,而我的无效!

在我发疯之前,我希望有人可以帮助解决这个问题!我敢肯定这是微不足道的。

4

3 回答 3

1

您的模型中似乎没有pblog_ref字段。看看创建 products 表的迁移,有pblog_ref字段吗?

编辑:好的,这行似乎是错误的:

<% form_for(@product, ProductBacklog.new) do |f| %>

@product它为您的not for创建表单ProductBacklog.new。尝试:

<% form_for(ProductBacklog.new) do |f| %>

我总是对form_for一些嵌套模型的参数有问题,所以我更喜欢使用accepts_nested_attributes_for然后创建子对象fields_for

在你的情况下form_for应该是这样的:

<% form_for ProductBacklog.new, :url => new_product_product_backlog_path(@product) do |f| %>

但是我没有检查它,它可能是错误的(正如我所说的那样,我总是对这些路径有问题)。

于 2010-05-30T07:49:39.860 回答
1

检查您的数据库,可能某些迁移失败并且您那里只有部分字段

您也可以先检查运行脚本/控制台,然后发出“ p Product.new ”。它将向您显示一个包含所有已知字段的空 Product 对象,例如:

#./script/console
Loading development environment (Rails 2.3.5)
>> p User.new
#<User id: nil, type: nil, login: nil, name: "", email: nil, crypted_password: nil, salt: nil, created_by_id: nil, created_at: nil, updated_at: nil, remember_token: nil, remember_token_expires_at: nil, male: true, dept_id: nil, deleted: nil>
=> nil
于 2010-05-30T07:59:22.167 回答
0

解决方案是更换线路:

<% form_for(@product, ProductBacklog.new) do |f| %>

和:

<% form_for [@product, ProductBacklog.new] do |f| %>

注意 form_for 后面的空格和方括号。

于 2010-05-30T14:42:17.470 回答