-1

我正在尝试使用一组嵌套资源创建新对象。当我发布我的 json 时,我得到:

ActiveRecord::AssociationTypeMismatch: FormLabel(#70306552439440) expected, got Hash(#70306480154920)

这是我的代码:

# 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

当我调试时,这些是参数:

0> params
=> {"name"=>"Address", "form_labels"=>[{"text"=>"foo"}], "format"=>"json", "controller"=>"forms", "action"=>"create", "form"=>{"name"=>"Address"}}

错误是:

ActiveRecord::AssociationTypeMismatch: FormLabel(#70237358584300) expected, got Hash(#70237286721880)
4

1 回答 1

0

错误在该行中:

collection :form_labels

它需要是:

collection :form_labels, decorator: FormLabelRepresenter, class: FormLabel

https://github.com/aptonick/roar-rails/issues/125#issuecomment-172175231

于 2016-01-18T17:38:38.337 回答