2012-02-21 26 views
1

我在創建一個嵌套表單時遇到問題,該表單可以使用nested_formsimple_form插件與以下域模型正常工作。我想我正在構造不正確的表單,或者是attr_accessible和accept_nested_attributes_for調用。nested_form問題,幾個級別深層的模型關聯

我有這樣的域模型與以下關聯:

company.rb

class Company < ActiveRecord::Base 
    has_one :subscription 

    attr_accessible :name, :subscription_attributes, :cycles_attributes, :payments_attributes 
    accepts_nested_attributes_for :subscription 
end 

subscription.rb

class Subscription < ActiveRecord::Base 
    belongs_to: company 
    has_many :cycles 

    attr_accessible :company_id, :cycles_attributes, :payments_attributes 
    accepts_nested_attributes_for :cycles 
end 

cycle.rb

class Cycle < ActiveRecord::Base 
    belongs_to :subscription 
    has_many :payments 

    attr_accessible :payments_attributes 
    accepts_nested_attributes_for :payments 
end 

付款。 rb

class Payment < ActiveRecord::Base 

    belongs_to :cycle 

end 

我的看法是這樣的:

<%= simple_nested_form_for company do |f| -%> 
    <%= f.input :name %> 

    <%= f.fields_for :subscription do |s| %> 
    #assorted fields for subscription 

    <%= s.fields_for :cycles do |c| %> 
     #assorted fields for cycle 
     <%= c.link_to_remove "[ - ] Erase Cycle" %> 

     <%= c.fields_for :payments do |p| %> 
     #assorted fields for payments 
     <%= p.link_to_remove "[ - ] Erase Payment" %> 
     <% end %> 

     <%= c.link_to_add "[ + ] New Payment", :payments %> 

    <% end %> 
    <%= s.link_to_add "[ + ] New Cycle", :cycles %> 

<% end %> 

公司控制器:

def new 
    @company = Company.new 
    @company.build_subscription 

    @categories = Category.find(:all) 

    respond_with @company 
end 

def create 
    @company = Company.new(params[:company]) 

    if @company.save 
    flash[:notice] = "Success" 
    else 
    flash[:error] = "Error #{@company.errors.full_messages}" 
    end 

    respond_with @company 
end 

當然,我簡化了意見和材料,還有一些驗證和領域我沒有顯示,但這些都不是問題。 當我提交我得到一些奇怪的錯誤形式,基本上所有信息在PARAMS通過,但它不是正確的嵌套:/

Parameters: {"utf8"=>"✓", "authenticity_token"=>"57wgXJinL6kql0F9CxShKpf11RhdMfqXnb6y8K/pDg0=", 
"company"=>{"name"=>"asdf12", 
    "subscription_attributes"=>{ 
    "cycles_attributes"=>{ 
     "0"=>{"plan_id"=>"1", "amount"=>"123", "months"=>"12", "_destroy"=>"false"} 
    }, 
    "0"=>{ 
     "0"=>{ 
     "payments_attributes"=>{ 
      "new_1329843584974"=>{"payment_type"=>"Efectivo", "amount"=>"123", "receipt_number"=>"1231", "note"=>"asdf asdf", "_destroy"=>"false" 
      } 
     } 
     } 
    } 
    } 
}, "commit"=>"Save"} 

WARNING: Can't mass-assign protected attributes: 0 

的錯誤是:

@messages={:"subscription.cycles.subscription"=>["can't be blank"], 
      :"subscription.cycles.payments"=>["can't be blank"]} 

也有一些是如何將屬性傳遞給應用程序,我的猜測是,當我點擊新的付款按鈕時,它會在窗體「外部」創建......有沒有人遇到類似的東西?

如果您需要更多信息,請告訴我。

回答