I am using backbone and trying to create a new object and I am running into this error. I thought rails would just ignore attributes that it doesn’t care about. i.e. the authenticity token isnt trying to be added to my object.
My controller:
def create
page = Page.find(params[:page_id])
branch = page.page_branches.build(params[:page_branch])
branch.form = page.form
if branch.valid?
page.page_branches << branch
redirect_to(edit_page_path(page, :anchor => "branch-panel"))
else
raise "#{branch.errors.map}"
end
end
The Error:
Processing by PageBranchesController#create as JSON
Parameters: {"page_branch"=>{"next_page_id"=>"KS5ad82889e04e5806e0455d5a81e81a9511",
"description"=>"linking", "utf8"=>"", "authenticity_token"=>"", "id"=>nil,
"page_id"=>"KS000c29724a16-u-2TQMP57-gYm-U", "trigger_type"=>"Always",
"base_options"=>"AllowAnonymous;BASE",
"question_options"=>"KS000c29724a16CfC2TQUL1--gxG-U", "keyword_options"=>"",
"qualification"=>"", "commit"=>""}, "page_id"=>"KS000c29724a16-u-2TQMP57-gYm-U"}
WARNING: Can't mass-assign protected attributes: id
Completed 500 Internal Server Error in 239ms
ActiveRecord::UnknownAttributeError (unknown attribute: utf8):
app/controllers/page_branches_controller.rb:21:in `create'
Thanks to the response below:
I added the attr_accessible on my rails model:
attr_accessible :page_id, :page_name, :next_page_id, :next_page_name,
:description, :trigger_type, :internal_qualification,
:order, :form_id, :qualification
By doing that, I get the following warnings, but the build succeeds
WARNING: Can't mass-assign protected attributes: utf8, authenticity_token, id, base_options, question_options, keyword_options, commit
ActiveRecord mass assignment is gonna try to use every key/value pair in the hash is provided and raise an UnknownAttributeError when unknown attributes are supplied via mass assignment.
Maybe checking out this code can help to confirm what I’m saying.
I think you have two options:
params[:page_branch]before use it in the mass assignment.Backbone.YourModel.toJSON()to not send weird attributes to the server.