2017-10-16 97 views
0

我試圖用繭寶石保存嵌套模型。這裏是PARAMS JSON:Rails 5:未經許可的參數

Parameters: { 
    "utf8" => "✓", 
    "authenticity_token" => "w/hwSu6yvT/R+6KFdoi7L6v3tDYJppiXyvQigBfTpD6EuTVz9P2CKImybxgip90YA0Yo7QKpID+IeCi2KLsy0g==", 
    "project" => { 
    "client" => "1", "name" => "Project 1", "instances_attributes" => { 
     "1508163439344" => { 
     "instances" => { 
      "user_id" => "user1", "password" => "[FILTERED]", "service_url" => "http://localhost:3001" 
     } 
     }, "1508163446859" => { 
     "instances" => { 
      "user_id" => "user2", "password" => "[FILTERED]", "service_url" => "http://localhost:3002" 
     } 
     } 
    } 
    }, 
    "commit" => "Create Project" 
} 

對於強參數代碼項目的控制器:

def project_params 
    params 
    .require(:project) 
    .permit(
    :client, :name, 
    instances_attributes: Instance.attribute_names.map(&:to_sym).push(:_destroy) 
) 
end 

現在,當我試圖挽救它顯示我:Unpermitted parameter: instances

我缺少什麼?

+0

你試過instance_attributes,奇異?此外,我認爲你必須通過:ID屬性的繭列表,但我不知道 –

+0

:id正在處理'Instance.attribute_names.map(&:to_sym)' –

+0

@RajMishra檢查我的解決方案。它應該爲你工作 – krishnar

回答

0

你有2個解決方案:

  • 允許所有instances_attributes內容:

    def project_params 
        params.require(:project).permit(:client, :name).permit!(:instances_attributes) 
    end 
    
  • 允許所有instances_attributes鍵:

    def project_params 
        instances_keys = (params[:project] || {})[:instances_attributes].keys 
        params.require(:project).permit(:client, :name, instances_attributes: instances_keys) 
    end 
    
+0

它仍然是'不允許的參數:實例' –

0

你有沒有注意到你的PARAMS。在instances_attributes裏面,您再次嵌套了名爲* instances的散列。所以你也需要允許他們。 更新您的project_params如下

def project_params 
    params 
    .require(:project) 
    .permit(
    :client, :name, 
    instances_attributes: [:instances=> Instance.attribute_names.map(&:to_sym).push(:_destroy)] 
) 
end 
+0

instances_attributes不是一個數組。這不起作用。 –

+0

@AntoineDewaele檢查控制檯。經過測試 – krishnar