2012-05-04 29 views
0

リリース用に以下のフォームがあります。トラックのフィールドは、リリースモデル内のネストされた属性として受け入れられます。親モデルのネストされた子属性へのアクセス?

<%= form_for(@release) do |f| %> 
<%= f.hidden_field :user_id, :value => current_user.id, :class => "text" %> 
<%= f.text_field :title, :class => "text" %> 

<%= f.fields_for :tracks do |builder| %> 
<%= render 'track_fields', :f => builder %> 
<% end %> 

<% end %> 

マイリリースモデルは含まれています

accepts_nested_attributes_for :tracks, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => :true 
    accepts_nested_attributes_for :releases_tracks 

    before_save :order_tracks 
    before_update :order_tracks 


    def order_tracks 
    releases_tracks.each { |t| t.position = track_attributes.position } 
    tracks.each { |t| t.user_id = user_id} 
    tracks.each { |t| t.label_id = label_id} 
    end 

    def track_attributes=(track_attributes) 
    track_attributes.each do |attributes| 
    tracks.build(attributes) 
    artists_tracks.build(attributes) 
    end 
    end 

すべては、私は、フォームのfields_for一部に入力された位置の値を取るしようとしている場所の下の行を除いて、うまく動作します。たとえば、親フォームuser_idから値にアクセスできますが、どのようにして子値にアクセスできますか?

releases_tracks.each { |t| t.position = track_attributes.position } 

ありがとうございます!

(注:私はこのためにacts_as_listを使用したくない)

答えて

0

試して使用する:

releases_tracks.each { |t| t.position = track_attributes[:position] } or 
releases_tracks.each { |t| t.position = track_attributes["position"] } 
+0

うーむ、それはtrack_attributes部分を好きではない、私は未定義のローカル変数を取得したり、メソッド 'track_attributes'error。私はこれを使って位置を1に設定できます:releases_tracks.each {| t | t.position = tracks [1]}だから私はそれにいくつかのバリエーションが必要なのだろうか? – Raoot

関連する問題