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)

4

1 回答 1

0

尝试使用:

releases_tracks.each { |t| t.position = track_attributes[:position] }  or 
releases_tracks.each { |t| t.position = track_attributes["position"] } 
于 2012-05-04T08:58:02.683 回答