2014-10-10 95 views
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! 

結果相同。我究竟做錯了什麼?

回答

0

你必須對轉讓

class Company 
    include Mongoid::Document 

    field :name, type: String 
    embeds_many :people, class_name: 'Person' 
    accepts_nested_attributes_for :people 
end 
+0

沒有影響接受nested_attributes。這些參數在碰到更新方法時已經遇到了這個問題。 – Jake 2014-10-10 13:24:24