簡體   English   中英

Rails:Rails 5 中不允許的參數

[英]Rails: Unpermitted parameter in Rails 5

首先,我只想在我發送到后端的當前對象中獲取一個對象。

我有這個簡單的JSON (從表單生成):

{
  "name": "Project 1",
  "project_criteria": [
    {
      "name": "Criterium 1",
      "type": "Type 1",
      "benefit": "1"
    },
    {
      "name": "Criterium 2",
      "type": "Type 2",
      "benefit": "3"
    }
  ]
}

我的classes

class Project < ApplicationRecord
  has_many :project_criteria
  accepts_nested_attributes_for :project_criteria
end

class ProjectCriterium < ApplicationRecord
  belongs_to :project
end

項目控制器:

def project_params
  params.require(:project).permit(:name,  project_criteria: [] )
end

但是我仍然無法訪問project_criteria參數,如下所示:

Started POST "/projects" for 127.0.0.1 at 2016-08-19 16:24:03 -0300
Processing by ProjectsController#create as HTML
  Parameters: {"project"=>{"name"=>"Project 1", "project_criteria"=>{"0"=>{"benefit"=>"1", "name"=>"Criterium 1", "type"=>"Type 1"}, "1"=>{"benefit"=>"3", "name"=>"Criterium 2", "type"=>"Type 2"}}}}
Unpermitted parameter: project_criteria # <-----------

注意:

順便說一句,我已經嘗試在has_manyaccepts_nested_attributes_for使用標准而不是標准在我看來,這是正確的,因為它應該是復數形式),但它也不起作用。

有人對此有解決方案嗎?

給您帶來問題的不是“標准”一詞的變形(盡管您可以添加自定義變形器來獲得您喜歡的單數和復數版本,如果您真的想要的話)。

問題是您必須明確允許嵌套對象的字段。

更改您當前的參數:

params.require(:project).permit(:name,  project_criteria: [] )

為此(對於單個嵌套對象):

params.require(:project).permit(:name,  project_criteria: [:name, :type, :benefit] )

由於您正在處理多個嵌套對象,因此您的情況有些復雜,因此您必須改為傳遞哈希:

params.require(:project).permit(:name,  { project_criteria: [:name, :type, :benefit]} )

我在處理 Rails 6 應用程序時遇到了這個問題。

我的應用程序包含一個User模型,該模型與Personal_Info模型具有一對一的關系

我的原始代碼是這樣的:

用戶模型

class User < ApplicationRecord 
  has_one :personal_info, class_name: 'PersonalInfo', dependent: :destroy
  accepts_nested_attributes_for :personal_info, allow_destroy: true
end

個人信息模型

class PersonalInfo < ApplicationRecord
  belongs_to :user
end

用戶控制器

class UsersController < ApplicationController

  def index
    @users = User.all
  end
  .
  .

  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation,
                                 personal_info_attributes: [:first_name,  
                                 :last_name, :phone, :gender, :dob, 
                                 :address, :city, :state, :country])
  end
end

問題是我沒有將 Personal_Info id 添加到接受的用戶參數(參數)中。

這是我修復它的方法

我只需要通過這種方式將 Personal_Info id添加到UsersController參數中:

用戶控制器

class UsersController < ApplicationController

  def index
    @users = User.all
  end
  .
  .

  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation,
                                 personal_info_attributes: [:id, :first_name,  
                                 :last_name, :phone, :gender, :dob, 
                                 :address, :city, :state, :country])
  end
end

另一種方法是通過這種方式將update_only選項添加到用戶模型中

class User < ApplicationRecord 
  has_one :personal_info, class_name: 'PersonalInfo', dependent: :destroy
  accepts_nested_attributes_for :personal_info, update_only: true, allow_destroy: true
end

僅此而已。

我希望這會有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM