I’ve got a nested form in Rails 3 as follows that works just fine during creation. In the edit stage, I’m getting WARNING: Can’t mass-assign protected attributes: type. The form shouldn’t be trying to set “type” and isn’t doing so as far as I can tell from the parameters.
class TagSetNomination < ActiveRecord::Base
belongs_to :pseud
belongs_to :owned_tag_set
has_many :fandom_nominations, :dependent => :destroy
has_many :character_nominations, :dependent => :destroy
has_many :relationship_nominations, :dependent => :destroy
has_many :freeform_nominations, :dependent => :destroy
accepts_nested_attributes_for :fandom_nominations, :character_nominations, :relationship_nominations, :freeform_nominations, {
:allow_destroy => true,
:reject_if => proc { |attrs| attrs[:tagname].blank? }
}
....
end
All of those nomination classes are subclasses of this base class:
class TagNomination < ActiveRecord::Base
belongs_to :tag_set_nomination
....
end
And here’s the relevant bit of my form:
<%= error_messages_for :tag_set_nomination %>
<%= form_for(@tag_set_nomination, :url => (@tag_set_nomination.new_record? ? tag_set_nominations_path(@tag_set) : tag_set_nomination_path(@tag_set, @tag_set_nomination)), :html => {:method => (@tag_set_nomination.new_record? ? :post : :put)}) do |f| %>
<h4><%= ts("Tag Nominations") %></h4>
<fieldset class="tagset">
<dl>
<% @tag_set_nomination.character_nominations.each_with_index do |character_nomination, index| %>
<%= f.fields_for :character_nominations, character_nomination do |nom_form| %>
<%= render 'tag_nominations', :nom_form => nom_form, :tag_type => 'character', :tag_nominations_counter => index %>
<% end %>
<% end %>
</dl>
</fieldset>
....
<% end %>
And some hopefully relevant bits from the log:
Started POST "/tag_sets/1/nominations/3" for 68.175.83.208 at 2011-08-23 02:59:08 +0000
Parameters: { ... "tag_set_nomination"=>{"character_nominations_attributes"=>{"0"=>{"tagname"=>"Sam", "parent_tagname"=>"", "tagnotes"=>"", "id"=>"12"}, "1"=>{"tagname"=>"Dean", "parent_tagname"=>"", "tagnotes"=>"", "id"=>"13"}, "2"=>{"tagname"=>"Yarbld", "parent_tagname"=>"Supernatural", "tagnotes"=>"some notes", "id"=>"16"}} ... }
SQL (0.1ms) SELECT COUNT(*) FROM `tag_nominations` WHERE `tag_nominations`.`type` = 'CharacterNomination' AND (`tag_nominations`.tag_set_nomination_id = 3)
...
SQL (0.2ms) ROLLBACK
Pseud Load (0.2ms) SELECT `pseuds`.* FROM `pseuds` WHERE (`pseuds`.user_id = 8)
CharacterNomination Load (0.3ms) SELECT `tag_nominations`.* FROM `tag_nominations` WHERE `tag_nominations`.`type` = 'CharacterNomination' AND (`tag_nominations`.tag_set_nomination_id = 3)
WARNING: Can't mass-assign protected attributes: type
WARNING: Can't mass-assign protected attributes: type
WARNING: Can't mass-assign protected attributes: type
And then I get dumped back to edit with no errors in the page. D:
Any ideas welcome! I am baffled.
Okay, I figured it out, sigh. It had zero to do with any of the code I pasted. I had a before_save callback that was setting a value to either true or false — and of course, when it returned false, the before_save callback died and therefore the save got rolled back.
facepalm