我有兩種型號。產品和產品價格(包括表格產品和產品價格),每種產品都有一個價格。我想爲兩個模型創建一個表單,但是在將解決方案複製到類似的場景之後,我的表單仍然沒有顯示價格字段。新形式的多種型號 - 導軌
class Product < ActiveRecord::Base
belongs_to :user
has_one :ProductPrice
accepts_nested_attributes_for :ProductPrice
end
class ProductPrice < ActiveRecord::Base
belongs_to :Product
end
class ProductsController < ApplicationController
def new
@product = Product.new
@product_price = @product.build_ProductPrice
end
end
<%= form_for @product, url: user_product_path do |f| %>
<div class="form-group">
<%= f.text_field :product_name, placeholder: 'name', class: 'form- control' %>
</div>
<% f.fields_for @product_price do |b| %>
<%= b.text_field :price, placeholder: 'Enter price', class: 'form-control' %>
<%end%>
<% end%>
任何想法?我是否正確地參考了模型?
編輯:固定。它需要是<%= fields_for .... 等號丟失
仍然得到相同的錯誤「未定義的方法'建立爲零:NilClass」 –