I am trying to create new objects with a collection of nested resources. When I POST my json, I get:
ActiveRecord::AssociationTypeMismatch: FormLabel(#70306552439440) expected, got Hash(#70306480154920)
Here's my code:
# form.rb
class Form < ActiveRecord::Base
has_many :form_labels
validates_presence_of :name
end
# form_label.rb
class FormLabel < ActiveRecord::Base
belongs_to :form
validates_presence_of :form
validates_presence_of :text
end
# form_representer.rb
module FormRepresenter
include Roar::JSON
property :id
property :name
collection :form_labels
end
# form_label_representer.rb
module FormLabelRepresenter
include Roar::JSON
property :text
end
# form_controller.rb
class FormsController < ApplicationController
include Roar::Rails::ControllerAdditions
def create
form = Form.new
consume!(form)
respond_with(form)
end
When I debug, these are the params:
0> params
=> {"name"=>"Address", "form_labels"=>[{"text"=>"foo"}], "format"=>"json", "controller"=>"forms", "action"=>"create", "form"=>{"name"=>"Address"}}
and the error is:
ActiveRecord::AssociationTypeMismatch: FormLabel(#70237358584300) expected, got Hash(#70237286721880)
The error is in the line:
It needs to be:
See https://github.com/apotonick/roar-rails/issues/125#issuecomment-172175231