[英]Accessing nested child attributes in parent model?
I have the following form for release, with fields for tracks being accepted as nested attributes within my release model. 我有以下发布形式,其中轨道字段在发布模型中被接受为嵌套属性。
<%= 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 %>
My release model contains: 我的发布模型包含:
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
Everything works well, except the line below where i'm trying to take the position value entered in the fields_for part of the form. 一切工作正常,除了下面的行,我试图获取在表单的fields_for部分中输入的位置值。 I can access values from the parent form, user_id for example, but how do I access the child values?
我可以从父表单中访问值,例如user_id,但是如何访问子值?
releases_tracks.each { |t| t.position = track_attributes.position }
Thanks all! 谢谢大家!
(Note: I don't want to use acts_as_list for this) (注意:我不想为此使用acts_as_list)
try to use: 尝试使用:
releases_tracks.each { |t| t.position = track_attributes[:position] } or
releases_tracks.each { |t| t.position = track_attributes["position"] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.