I'm trying to get my backbone associations working inside a rails app , and I'm having difficulty when trying to update existing models. Specifically, Rails throws the following error:
Started PUT "/posts/2" for 127.0.0.1 at 2012-01-04 02:36:14 +1000
Processing by PostsController#update as JSON Parameters: {"post"=>{"content"=>"Seconderona", "created_at"=>"2012-01-03T10:51:09Z", "id"=>2, "title"=>"Second test post", "updated_at"=>"2012-01-03T10:51:09Z", "comments"=>[{}]}, "id"=>"2"} Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "2"]] WARNING: Can't mass-assign protected attributes: id Completed 500 Internal Server Error in 15msActiveRecord::AssociationTypeMismatch (Comment(#70104367824560) expected, got ActiveSupport::HashWithIndifferentAccess(#70104367278120)):
app/controllers/posts_controller.rb:62:inblock in update'
update'
app/controllers/posts_controller.rb:61:in
A few things:
This is triggered on (for example):
c = window.router.comments.models[0]
c.save({content: 'Changed content'})
Also, yes, 'accepts_nested_attributes_for' is present in the model.
The (offending) code below is taken pretty much verbatim from thougtbot's "backbone on rails" ebook, and I've also tried following the documentation for the backbone-relational gem. Both raise this error. Any ideas appreciated, code below
RAILS 'POST' MODEL
class Post < ActiveRecord::Base
has_many :comments
accepts_nested_attributes_for :comments
def as_json(options = nil)
super((options || {}).merge(include: { comments: { only: [content] } } ))
end
end
RAILS 'COMMENT' MODEL
class Comment < ActiveRecord::Base
belongs_to :post
accepts_nested_attributes_for :post
def as_json(options = nil)
super((options || {}).merge(include: { post: { only: [:title, :content]}}))
end
end
BACKBONE POST CONTROLLER
class Backbonerelationaldemo.Models.Post extends Backbone.Model
paramRoot: 'post'
initialize: () ->
comments = new Backbonerelationaldemo.Collections.CommentsCollection
comments.reset(@get('comments'))
@setComments(comments)
setComments: (comments) ->
@comments = comments
class Backbonerelationaldemo.Collections.PostsCollection extends Backbone.Collection
model: Backbonerelationaldemo.Models.Post
url: '/posts'
BACKBONE COMMENTS CONTROLLER
class Backbonerelationaldemo.Models.Comment extends Backbone.Model
paramRoot: 'comment'
initialize: () ->
if (@has('post'))
@setPost(new Backbonerelationaldemo.Models.Post(@get('post')))
setPost: (post) ->
@post = post
class Backbonerelationaldemo.Collections.CommentsCollection extends Backbone.Collection
model: Backbonerelationaldemo.Models.Comment
url: '/comments'
For those who are Googling...
While the current accepted answer certainly works, the following was how I worked out my problem (very similar, but slightly different):
I was using a nested form, with models nested 4 layers deep. In my controller which was rendering the form, I needed to build out the form.
This allowed the params to include
params[:survey][:questions_attributes]
. Rails renamed the parameters for me because I informed it ahead of time.Hope this helps!
I dealt with the same issue recently. It's actually not a HashWithIndifferentAccess error: it has to do with how
accepts_nested_attributes_for
expects params.When you declare
accepts_nested_attributes_for :comments
, Rails looks for a parameter callcomments_attributes
on the incoming params.The problem is that your JSON representation coming from Backbone has a
"comments"
property instead of a"comments_attributes"
property.You could fix it on the Backbone side by adding a toJSON function to your Post model:
Or you could handle it in your Rails controller:
Hopefully this helps.