我目前正在尝试使用嵌套表单在您创建歌曲时将类别标签添加到歌曲中。目前,每次我提交表单时它都会引发大量分配错误,尽管我已经输入了我认为正确的属性可访问特性。显然我在某个地方出错了,所以如果有人能为我指出这一点,那就太好了。表格是:
<%= nested_form_for(@song) do |f| %>
...
<%= f.fields_for(@song.categorizations.build) do |cat| %>
<%= cat.label :category_id, "TAG" %>
<%= cat.select :category_id, options_for_select(Category.all.collect {|c| [ c.tag, c.id ] },
{ :include_blank => true }), prompt: "" %>
<%end%>
<%= f.submit "Save" %>
<% end %>
这里的相关模型是:
class Song < ActiveRecord::Base
attr_accessible :artist, :title, :categorizations_attributes
has_many :categorizations, dependent: :destroy
has_many :categories, through: :categorizations
accepts_nested_attributes_for :categorizations
歌曲控制器如下所示:
def create
@song = Song.new(params[:song])
if @song.save
flash[:success] = "Song successfully added to library"
redirect_to @song
else
#FAIL!
render 'new'
end
end
最后引发的错误是:
ActiveModel::MassAssignmentSecurity::Error in SongsController#create
Can't mass-assign protected attributes: categorization
预先感谢您的任何帮助!