在我的应用程序中:
A song has many setlists through allocations
A setlist has many songs through allocations
allocations belong to setlists and songs
我正在尝试使用以下形式将现有库中的歌曲添加到设置列表中:
<% @songs.each do |song| %>
<tr>
<%= nested_form_for(@setlist.allocations.build(song_id: song.id)) do |f| %>
<td><%= song.title %></td>
<td><%= song.artist %></td>
<td><%= song.key %></td>
<td>
<div><%= f.hidden_field :song_id %></div>
<%= f.submit "ADD", class: "btn btn-small btn-primary" %>
<% end %>
</td>
</tr>
<% end %>
在我的设置列表控制器中,我有:
def edit
@songs = Song.all(order: 'title')
@setlist = Setlist.find(params[:id])
@setSongs = @setlist.songs
@allocations = Allocation.where("setlist_id =?", @setlist)
end
def update
@setlist = Setlist.find(params[:id])
song = Song.find(params[:song_id])
@setlist.allocate!(song)
if @setlist.update_attributes(params[:setlist])
# Handle a successful update.
flash[:success] = "SAVED!"
redirect_to setlists_path
else
render 'edit'
end
end
分配方法在 setlist 模型中指定为:
def allocate!(song)
allocations.create!(song_id: song.id)
end
目前,每当我单击将歌曲添加到设置列表时,它都会返回以下内容。
SetlistsController 中的 NoMethodError #update undefined method `id' for :song:Symbol
该错误还很奇怪,它仅对表中的第一条记录执行上述错误,而所有其他错误都在未添加记录时呈现“错误”页面。
任何指针将不胜感激。首先十分感谢