2
强大的参数我在Mongoid/Rails的1-N关系:Mongoid嵌入文档和Rails不工作
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总是缺少:人物属性。 Rails日志说:
Parameters: {"id"=>"5436fbc64a616b5240050000", "name"=>"GLG", "people"=>[{"first_name"=>"Jake"}], "company"=>{"name"=>"GLG"}}
我没有收到“未经许可的参数”警告。我已经尝试了允许人员领域的所有可能的方式,但仍然没有包括在内。
params.require(:company).permit!
结果相同。我究竟做错了什么?
没有影响接受nested_attributes。这些参数在碰到更新方法时已经遇到了这个问题。 – Jake 2014-10-10 13:24:24