0

让我自己都对我认为的双循环感到困惑,不知道如何解决它。谁能救我免于完全发疯?

这在我的显示视图中有效:

<% @releases_tracks_temp.each do |releases_track| %>
  <tr>
    <td><%= releases_track.track.id %> / <%= releases_track.position %></td>
    <td><%= releases_track.track.name %>
        <%= releases_track.track.artists.map { |a| a.name}.join (", ") %></td>
    <td><%= releases_track.track.isrc %></td>
    <td><%#= link_to image_tag("icons/delete.png"), releases_track, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>

我正在尝试使用自定义操作和视图为多个轨道呈现表单:

<%= form_for @release do |f| %>
  <%= f.fields_for :tracks do |builder| %>
     <%= render 'track_fields', :f => builder %>
  <% end %>
  <%= link_to_add_fields "Add track", f, :tracks %>

然后是字段本身:

<% @releases_tracks_temp.each do |releases_track| %>
  <%= f.text_field :name, :class => "text" %>
  <%= f.text_field :artist_tokens, "data-pre" => releases_track.track.artists.map(&:attributes).to_json, :class => "track_artist_tokens" %>
  <%= f.text_field :version, :class => "text" %>
  <%= f.text_field :isrc, :class => "text" %>
  <%= f.select(:asset_tier, options_for_select([['Front', 'Front'], ['Mid', 'Mid'], ['Back', 'Back']]), {}, :class => "tier-select") %>
  <%= f.text_field :preview_start, :class => "text small" %>
  <%= f.check_box :parental_advisory %>
  <%= f.check_box :available_separately, {:checked => true} %>
  <%= f.check_box :_destroy %>
<% end %>

我最终让它循环,但是这个数字有很多轨道,例如 5 轨道显示 5 次!所以25个结果。我想我看到了一个双循环,但我不确定如何解决这个问题,并且仍然可以通过以下方式访问每个轨道的(多个)艺术家:

releases_track.track.artists.map(&:attributes)

有任何想法吗?

哦,@releases_tracks_temp 在我的发布控制器中定义为:

 @releases_tracks_temp = @release.releases_tracks.find(:all, :order => "position") 

轨道被接受为嵌套在版本下。

提前致谢!!!

4

1 回答 1

1

对于每个版本,每个轨道打印列表。无需在轨道上循环两次

有一个表格而不是 2 个

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

编辑 - 以这种方式尝试

<%= form_for([@release, @release.tracks.build]) do |f| %>
  <%= f.text_field :name, :class => "text" %>
  <%= f.text_field :artist_tokens, "data-pre" => releases_track.track.artists.map(&:attributes).to_json, :class => "track_artist_tokens" %>
  <%= f.text_field :version, :class => "text" %>
  <%= f.text_field :isrc, :class => "text" %>
  <%= f.select(:asset_tier, options_for_select([['Front', 'Front'], ['Mid', 'Mid'], ['Back', 'Back']]), {}, :class => "tier-select") %>
  <%= f.text_field :preview_start, :class => "text small" %>
  <%= f.check_box :parental_advisory %>
  <%= f.check_box :available_separately, {:checked => true} %>
  <%= f.check_box :_destroy %>

  <%= link_to_add_fields "Add track", f, :tracks %>
<% end %>
于 2012-04-30T17:54:04.137 回答