2012-07-11 19 views
0

我知道一個事實(通過控制檯檢查)輸出應該是三首歌曲,但出於某種原因,我不斷收到第四首歌曲添加到每個頁面。這首歌每次都會有所不同,我對此感到困惑。任何幫助深表感謝!Rails在視圖中返回奇怪的無效信息,但不是控制檯

在我的控制器

def edit 
    @songs = Song.all(order: 'title') 
    @song = Song.find(params[:id]) 
    @setlist = Setlist.find(params[:id]) 
    @allocations = @setlist.allocations 
    end 

完整視圖代碼,如要求:

首先,鑑於實際的編輯頁面:

<h1>Edit a Setlist</h1> 
<div class="row"> 
    <div class="span8"> 
     <%=form_for(@setlist) do|f|%> 

     <%=f.label :date, "Set a date" %> 
     <span><%=f.date_select :date%><span> 

     <%=f.label :morning, "Morning" %> 
     <%=f.radio_button :morning, true %> 
     <%=f.label :morning, "Evening" %> 
     <%=f.radio_button :morning, false %> 

     <h2>Library</h2> 

     <div> 
      <%= render 'library' %> 
      <%= render 'currentSet' %> 
     </div> 

     <%=f.submit "Save Setlist", class: "btn btn-large btn-primary" %> 
     <% end %> 
    </div> 
</div> 

電流設定部分(忽略庫部分,因爲它是一個沒有添加任何東西的嵌套形式):

<div id="currentSet"> 
    <h2>Current Setlist</h2> 
    <table class= "table table-striped table-bordered"> 
     <thead> 
     <th>Title</th> 
     <th>Artist</th> 
     <th>Root Key</th> 
     <th>Remove</th> 
     </thead> 
     <tbody> 
     <% if(@allocations.empty?) %> 
      <tr><td>NO SONGS</td></tr> 
     <% else %> 
      <%= render @allocations %> 
     <%end%>  
     </tbody> 
    </table> 
    </div> 

及配置:

<tr> 
    <td><%= allocation.song.title %></td> 
    <td><%= allocation.song.artist %></td> 
    <td><%= allocation.song.key %></td> 
    <td><%= link_to "Remove Song", allocation, method: :delete %></td> 
</tr> 

我不知道這是否是相關的,但我還添加了實際呈現的HTML檢查:

<tr> 
    <td>S</td> 
    <td>A</td> 
    <td>K</td> 
    <td><a href="/allocations/7" data-method="delete" rel="nofollow">Remove Song</a>... 
</tr><tr> 
    <td>S</td> 
    <td>A</td> 
    <td>K</td> 
    <td><a href="/allocations" data-method="delete" rel="nofollow">Remove Song</a></td> 
</tr> 

我發現奇怪的部分是最後一個條目與其他所有條目的不同之處在於,url最後一個沒有分配ID ie/allocations/9,而最後一個沒有分配ID。

+0

你在哪裏告訴它顯示三首歌而不是四首?我沒有看到任何循環。數據庫只有三首歌嗎?那麼你如何獲得第四名? – Conner 2012-07-11 19:21:36

+0

我告訴它顯示已經分配給setlist的所有歌曲。不過,我知道一個事實,即數據庫只有每個設置列表分配了三首歌曲(我已經使用控制檯確認過)。 – TangoKilo 2012-07-11 19:23:24

+0

's.allocations.count (0.2ms)SELECT COUNT(*)FROM「allocations」WHERE「allocations」。「setlist_id」= 1 => 3' – TangoKilo 2012-07-11 19:24:32

回答

3

就關閉這個一關則:

def edit 
    @songs = Song.all(order: 'title') 
    @song = Song.find(params[:id]) 
    @setlist = Setlist.find(params[:id]) 
    @allocations = @setlist.allocations 
    end 

使用適當的:ID在你的歌聲和SETLIST儀(即不是同一個)。

相關問題