我在Mongoid / Rails中有1-N关系:
class Company
include Mongoid::Document
field :name, type: String
embeds_many :people, class_name: 'Person'
end
class Person
include Mongoid::Document
field :first_name, type: String
embedded_in :company, class_name: 'Company', inverse_of: 'people'
end
现在,我可以在控制台中成功创建公司;例如:
> c = Company.new(name: 'GLG', :people => [{first_name: 'Jake'}]) # OK!
> c.people # OK!
然后我有一个JSON API控制器来更新公司,方法如下:
# PUT /api/companies/:id
def update
if Company.update(company_params)
# ... render JSON
else
# ... render error
end
end
private
def company_params
params.require(:company).permit(:name, :people => [:first_name])
end
现在,当PUT请求来自前端时,company_params总是缺少:people属性。 Rails日志说:
Parameters: {"id"=>"5436fbc64a616b5240050000", "name"=>"GLG", "people"=>[{"first_name"=>"Jake"}], "company"=>{"name"=>"GLG"}}
我没有收到“未经许可的参数”警告。我已经尝试了所有可以想象的方式来允许人员领域,但它仍然没有被包括在内。
params.require(:company).permit!
结果相同。我做错了什么?
答案 0 :(得分:0)
您必须在转让时接受nested_attributes
class Company
include Mongoid::Document
field :name, type: String
embeds_many :people, class_name: 'Person'
accepts_nested_attributes_for :people
end