我正在尝试构建一个简单的产品积压应用程序来自学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我已经检查了我在Creating a weblog in 15 minutes with Rails 2 screencast上做了什么,原则上我似乎和他做了同样的事情-只有他的嵌套注释表单可以工作,而我的不能!
在我发疯之前,我希望有人能帮上忙!我确定这是件微不足道的事。
发布于 2010-05-30 22:42:17
解决方案是替换这行代码:
<% form_for(@product, ProductBacklog.new) do |f| %>通过以下方式:
<% form_for [@product, ProductBacklog.new] do |f| %>注意form_for后面的空格和方括号。
发布于 2010-05-30 15:49:40
您的模型中似乎没有pblog_ref字段。查看一下迁移创建products表,是否有pblog_ref字段?
编辑:好的,看起来这行是错的:
<% form_for(@product, ProductBacklog.new) do |f| %>它为你的@product而不是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-30 15:59:22
检查您的数据库,可能某些迁移失败,而您的数据库只有部分字段
您也可以先运行script/console,然后发出"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>
=> nilhttps://stackoverflow.com/questions/2937863
复制相似问题