I know for a fact (having checked through the console) that the output should be three songs, but for some reason I keep getting a fourth song added to every page. The song is different on every occasion, and I’m baffled as to why this is happening. Any help is much appreciated!
In my controller
def edit
@songs = Song.all(order: 'title')
@song = Song.find(params[:id])
@setlist = Setlist.find(params[:id])
@allocations = @setlist.allocations
end
The full view code, as requested:
First the view for the actual edit page:
<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>
The current set partial (ignore the library partial, as it’s a nested form with nothing added yet):
<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>
And allocations:
<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>
I’m not sure if this is relevant but I’ve also added the actual rendered html for examination:
<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>
The part I’m finding wierd is that the last entry differs from all the others in that the url doesn’t have an allocation id i.e. /allocations/9 in the second last one vs /allocations in the last one.
Just to close this one off then:
Use the appropriate :id’s in your Song and Setlist finders (i.e. not the same one).