2012-02-21 16 views
1

nested_formsimple_formプラグインを使用して、次のドメインモデルで正しく動作するネストされたフォームを作成できません。私はフォームを間違って構築していると思います。あるいはattr_accessibleとaccepts_nested_attributes_forの呼び出しです。複数のレベルのネストされたモデルの関連付けの問題

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"]} 

何かがあります属性がアプリにどのように渡されているのか迷っているので、新しい支払いボタンをクリックすると、フォームの外に作成されます。誰かが似たようなことに遭遇しましたか?

これ以上の情報が必要な場合は、お知らせください。

答えて

0

問題はnested_formプラグイン自体にありました。

has_one関係とそのネストされたモデルを子として持つ場合、nested_formはフォームの生成に問題がありました。

私はこのプルリクエストをチェックアウトし、変更に対応するファイル

Issue #124 in the nested_form repo

the patch that fixes the issue

このコードは、最終的にメインリポジトリにマージする必要があるパッチを適用。

関連する問題