I’ve currently got three models: Songs have many Setlists and vice versa, through the Allocations model.
I’m trying to use a nested form to add existing songs to a setlist.
My current view for the nested form:
<div>
<%=form_for @allocation do|builder|%>
<%=builder.label :song_id, "Pick a song" %>
<%= builder.hidden_field :setlist_id, value: @setlist.id %>
<%= builder.select(:song_id, options_for_select(@selections), {}, {multiple: true, size: 7}) %>
<%=builder.submit "Add Song", class: "btn btn-large btn-primary" %>
<% end %>
</div>
and my controller for editing setlists:
def edit
@songs = Song.all(order: 'title')
@setlist = Setlist.find(params[:id])
@allocations = @setlist.allocations
@allocation = Allocation.new
@selections = Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id ] }
end
def update
@setlist = Setlist.find(params[:id])
@selections = Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id] }
@allocations = @setlist.allocations
@allocation = Allocation.new
params[:allocation][:song_id].reject! { |c| c.empty? }
if @setlist.update_attributes(params[:setlist])
if @allocation.save
flash[:success] = "SETLIST SAVED!"
redirect_to setlist_path(@setlist)
else
flash[:fail] = "Setlist not saved"
render 'edit'
end
else
flash[:fail] = "FAIL!"
render 'edit'
end
end
Whenever I submit the form to add a song to the setlist I get an error back saying:
Validation failed: Setlist can't be blank, Song can't be blank
All the parameters appear to being passed correctly so I’m stumped. Here’s the parameters returned:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"ThIXkLeizRYtZW77ifHgmQ8+UmsGnDhdZ93RMIpppNg=",
"setlist"=>{"date(1i)"=>"2012",
"date(2i)"=>"7",
"date(3i)"=>"11",
"morning"=>"false"},
"allocation"=>{"setlist_id"=>"1",
"song_id"=>["5"]},
"commit"=>"Add Song",
"id"=>"1"}
Thank you for any help in advance
You’re allowing multiple selections in the
:song_idfield, one option of which I imagine has a blank value. That option and one other must be getting selected, causing the["", 13]response.This will clean blank entries from that param. This should be placed in the
updatemethod anywhere before this lineAs for the validation error, I assume it’s coming from the
Allocationsince that’s what the form is for.Not knowing all of the attributes have attributes here that require values, like
:set_list_idand:song_id. You’re trying to persist anAllocationto the database without first setting any of it’s attributes. This is the likely source of the validation issues you’re having.Edit:
A nested form in rails is a set of form fields for an object associated with the object for the parent form. Notice how this form has
fields_forcalled on theperson_formobject. This will result in nested parameters likeparam[:person][:children][:name].In the
updatemethod for this you could have something as simple asThe
update_attributeson Person can manage the creation, updating, and saving of it’schildrenassociation for you withaccepts_nested_attributes_forI think this is what you’re after, and as such you may need to rethink your view and the
updatemethod accordingly. This is relatively tricky stuff for an absolute beginner to rails; just keep going back to the documentation (it’s very well written) and asking for help here.