2
OK,這是奇怪的,我基本上有以下類:的Rails 3 - Fields_for嵌套屬性沒有出現在形式
class PriceProfile < ActiveRecord::Base
has_many :prices
has_many :price_profile_date_ranges
attr_accessible :name, :price_profile_date_ranges_attributes
accepts_nested_attributes_for :price_profile_date_ranges
}
class PriceProfileDateRange < ActiveRecord::Base
attr_accessible :end_date, :price_profile_id, :start_date, :prices, :prices_attributes
has_many :prices, :dependent=>:destroy
belongs_to :price_profile
accepts_nested_attributes_for :prices
}
class Price < ActiveRecord::Base
attr_accessible :price_profile_date_range_id, :price_profile_id, :product_id, :value
belongs_to :price_profile
belongs_to :price_profile_date_range
belongs_to :product
}
一個價格配置文件定義特定的產品,其價格隨時間變化的定價方案。價格應用的日期範圍存儲在price_profile_date_range表中,最後價格表保存所有價格。我使用以下控制器&視圖來創建此處的表單,以便在創建日期範圍時設置價格。基本上這個表格有一個開始和結束日期字段和一個網格,即它將有一個textebox列表,以針對所有產品輸入價格。
這是視圖:
.row
.span9
= simple_form_for(@price_profile_date_range, :class=>'well') do |f|
.form-inputs
= f.input :start_date, :required => true, :as => :string, :input_html =>{:class=>'datepicker'}
= f.input :end_date, :required => true, :as => :string, :input_html =>{:class=>'datepicker'}
= f.input :price_profile_id, :as=>:hidden
%table.table.table-bordered.table-condensed.table-striped
%tr
%td
- @products.each do |product|
%td
=product[:name]
%td
- f.fields_for(:prices) do |price_element|
= price_element.input :value, :class=>'span1'
= price_element.input :price_profile_id, :as=>:hidden
= price_element.input :price_profile_date_range_id, :as=>:hidden
= price_element.input :product_id, :as=>:hidden
.form-actions
= f.button :submit
這個心不是完全相同的最終形式 - 問題是,f.fields_for線似乎並沒有執行。在控制器中,我使用一組價格初始化@price_profile_date_range對象。如果我做了檢查,即使在視圖中顯示所有價格對象,但fields_for根本不會執行。我在這裏真的很糟糕。
我有一個類似的問題,但不使用haml。我改變了 <%f.fields_for:users do | builder | %> 至 <%= f.fields_for:users do | builder | %> 和那爲我工作。 – 2012-10-31 15:56:59