I'm trying to use a nested form at the moment to add category tags to a song as you create the song. At the moment it's throwing a mass assignment error every time I submit the form despite the fact that I've put in what I believe to be the correct attribute accessible characteristics. Obviously I've gone wrong somewhere so it'd be great if someone could point that out for me. The form is:
<%= 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 %>
The relevant model here is:
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
The song controller looks like this:
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
Finally the error being raised is:
ActiveModel::MassAssignmentSecurity::Error in SongsController#create
Can't mass-assign protected attributes: categorization
Thank you in advance for any help!