2012-02-27 3 views
5

ネストされたモデルフォームを生成できません。ここで fields_forがhas_many関係で動作するようにする

は私のモデルです:私はfields_for、Iドンしようとすると、フォームで、しかし

def new 
    @workout = Workout.new 
    3.times { @workout.scores.build } 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @wod } 
    end 
end 

:ワークアウトコントローラで

class Workout < ActiveRecord::Base 
    has_many :scores 
    has_many :users, :through => :scores 
    accepts_nested_attributes_for :scores 
end 

class Score < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :workout 
end 

class User < ActiveRecord::Base 
    has_many :scores 
    has_many :workout, :through => :scores 
end 

、ここで私は新しいアクションのために持っているものです何も取得できません:

<% f.fields_for :scores do |builder| %> 
    <p> 
     <%= builder.label :score %><br /> 
     <%= builder.text_field :score %> 
    </p> 
<% end %> 

私は間違っていますか?あなたのWorkoutモデルに以下を追加

+0

「Wod」とは何ですか?なぜあなたはWorkoutsコントローラでそれを作成していますか? –

+0

それはタイプミスでした。一定! – Huey

答えて

6

Rails 3では、<%fields_for ...%>の代わりに<%= fields_for ...%>を使用する必要があります。

0

試してみてください。

attr_accessible :scores_attributes 

accepts_nested_attributes_for :scores 

あなたはそれが有効でない限り、スコアが構築されない、そしてそれはあなたができる関係を通じて破壊することができていることを確認するにはただ、作成するスコアのために必要とされ、関連する分野で:fieldを切り替える

attr_accessible :scores_attributes 

accepts_nested_attributes_for :scores, reject_if: proc { |a| a[:field].blank? }, allow_destroy: true 
validates_associated :scores 

:に展開されます。

+0

愚かなことに、問題は<%fields_for ...%>の代わりに<%= – Huey

関連する問題