问题描述
让我自己都对我认为的双重循环感到困惑,并且不知道如何解决它.有人能救我免于发疯吗?
Gotten myself all confused with what I think is a double loop and don't know how to fix it. Can anyone save me from going completely mad please?
这在我的表演视图中有效:
This works in my show view:
<% @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 %>
我正在尝试使用自定义操作为多个轨道呈现表单,并使用以下视图进行查看:
I'm trying to render a form for multiple tracks with a custom action and view using:
<%= 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个结果.我想我看到了一个双循环,但是我不确定如何解决这个问题,仍然可以通过以下方式访问每首曲目的(多个)艺术家:
I end up getting it looping however many tracks there are by that number, for example 5 tracks are displayed 5 times! So 25 results. I think I see a double loop but i'm not sure how to fix this and still have access to the (multiple) artists for each track via:
releases_track.track.artists.map(&:attributes)
有什么想法吗?
哦,@ releases_tracks_temp在我的发布控制器中定义为:
Oh, @releases_tracks_temp is defined in my releases controller as:
@releases_tracks_temp = @release.releases_tracks.find(:all, :order => "position")
轨道被视为嵌套在发行版之下.
Tracks are accepted as nested under releases.
提前谢谢!
推荐答案
对于每个发行版,为每个轨道打印列表.无需遍历曲目两次
For each release, for each track print the list. No need to loop over tracks twice
使用一种形式而不是2
Have one form instead of 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 %>
这篇关于怪异的循环行为-这里发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!