怪异的循环行为-这里发生了什么?(Weird looping behaviour - what's happening here?)

15 IT屋

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 %>

Then the fields themselves:

<% @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 %>

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)

Any ideas?

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.

Thanks in advance!!!

解决方案

For each release, for each track print the list. No need to loop over tracks twice

Have one form instead of 2

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

Edit - Try it this way

<%= 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 %>

让我自己都对我认为的双重循环感到困惑,并且不知道如何解决。有人能救我免于发疯吗?



这在我的表演视图中有效:



 <%@ releases_tracks_temp.each做| 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 => ‘确定吗?’,:method => :delete%< / td>
< / tr>
<%end%>


我正在尝试使用自定义操作呈现多条轨道的表单,并使用以下视图进行查看:



 <%= form_for @release do | f | %> 
<%= f.fields_for:tracks do | builder | %>
<%=渲染" track_fields",:f =>生成器%>
<%end%>
<%= link_to_add_fields"添加曲目",f,:tracks%>


然后填写字段:



 <%@ releases_tracks_temp.each做| releases_track | %> 
<%= f.text_field:name,:class => "文本"%>
<%= f.text_field:artist_tokens," data-pre" => releases_track.track.artists.map(&:attributes).to_json,:class => " track_artist_tokens"%>
<%= f.text_field:version,:class => "文本"%>
<%= f.text_field:isrc,:class => "文本"%>
<%= f.select(:asset_tier,options_for_select([['Front','Front'],['Mid','Mid'],['Back','Back']]), {},:class =>" tier-select")%>
<%= f.text_field:preview_start,:class => "文字小"%>
<%= 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 =>"位置")


轨道被视为嵌套在版本下。



预先感谢!


解决方案

对于每个发行版,为每个曲目打印列表。无需循环跟踪两次



使用一种形式而不是2



 <%= form_for @release做| f | %> 
<%#= f.fields_for:tracks do | builder | %>
<%=渲染" track_fields",:f => f%>
<%#结束%>
<%end%>


编辑-尝试这种方式



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

<%= link_to_add_fields"添加曲目",f,:tracks%>
<%end%>

本文地址:IT屋 » 怪异的循环行为-这里发生了什么?