2012-05-04 124 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一部分進入到的位置值線。例如,我可以從父窗體訪問值,但是如何訪問子值?

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

Hmmmm,它不喜歡的track_attributes部分,我得到一個未定義的局部變量或方法'track_attributes'錯誤。我可以使用這個設置位置爲1:releases_tracks.each {| t | t.position = tracks [1]}所以我想知道我是否需要一些變化? – Raoot