私は現在3つのモデルを持っています:割り当てモデルを通して、曲には多くのセットリストがあり、その逆もあります。
ネストされたフォームを使用して、既存の曲をセットリストに追加しようとしています。
ネストされたフォームの現在のビュー:
<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>
セットリストを編集するための私のコントローラー:
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
セットリストに曲を追加するためにフォームを送信するたびに、次のようなエラーが返されます。
Validation failed: Setlist can't be blank, Song can't be blank
すべてのパラメーターが正しく渡されているように見えるので、困惑しています。返されるパラメータは次のとおりです。
{"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"}
よろしくお願いします