我试图构建一个简单的产品积压应用程序来教自己的Rails。对于每个产品,可能会有多个产品积压条目,因此我想创建一个产品视图,其中显示产品信息,产品的所有积压条目,并包含用于添加更多积压条目的嵌套表单。Rails中的嵌套表单问题:显示中的NoMethodError
一切正常,直到我尝试的形式添加到视图,然后导致以下错误:
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
我查了一下我对付Creating a weblog in 15 minutes with Rails 2 screencast,原则上我看起来和他一样 - 只有他的嵌套评论形式有效,而我的不是!
我希望有人能帮助这个,在我疯了之前!我相信这是微不足道的。
是的,有。对不起,我忘了在模式中包含表格的细节(现在已经添加上面)。所有的领域都在那里。当我删除引用pblog_ref的行时,错误会移至f.text_field:product行。 – Brian 2010-05-30 07:54:49
@brianheys:我编辑了我的答案。它解决了问题吗? – klew 2010-05-30 11:12:26
感谢您的帮助。我现在设法解决了这个问题。请参阅下面的答案。 – Brian 2010-05-30 15:19:52