Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 Can';t批量分配受保护的属性:文档_Ruby On Rails 3_Nested Attributes_Mass Assignment - Fatal编程技术网

Ruby on rails 3 Can';t批量分配受保护的属性:文档

Ruby on rails 3 Can';t批量分配受保护的属性:文档,ruby-on-rails-3,nested-attributes,mass-assignment,Ruby On Rails 3,Nested Attributes,Mass Assignment,我有两个模型和员工文件,如下所示: Employee.rb class Employee < ActiveRecord::Base has_one :document #,dependent: :destroy attr_accessible :age, :dob, :empNo, :first_name, :gender, :last_name, :middle_name, :document_attributes accepts_nested_attributes_for

我有两个模型和员工文件,如下所示:

Employee.rb

class Employee < ActiveRecord::Base
  has_one :document #,dependent: :destroy
  attr_accessible :age, :dob, :empNo, :first_name, :gender, :last_name, :middle_name, :document_attributes
  accepts_nested_attributes_for :document
  validates :first_name, presence: true , length: { maximum: 50 }
  validates :empNo, presence: true, uniqueness:{ case_sensitive: false }
  validates_length_of :empNo, :minimum => 5, :maximum => 5

  #before save { |user| user.email = email.downcase }
end
class Document < ActiveRecord::Base
  belongs_to :employee,foreign_key: "empno"
  attr_accessible :idate, :iedate, :insuranceno, :iqamano, :iqedate, :iqidate, :passportno, :pedate, :pidate, :vedate, :vidate, :visano
end
当我创建一个新员工时,我得到以下错误

 ActiveModel::MassAssignmentSecurity::Error in EmployeesController#create
 Can't mass-assign protected attributes: document
requsts参数很好,如下所示

{"utf8"=>"✓",
 "authenticity_token"=>"vXSnbdi+wlAhR5p8xXvTWhi85+AVZgOZufClx73gc8Q=",
 "employee"=>{"empNo"=>"11111",
 "first_name"=>"Thaha",
 "middle_name"=>"Muhammed",
 "last_name"=>"Hashim",
 "age"=>"25",
 "gender"=>"M",
 "dob(1i)"=>"2014",
 "dob(2i)"=>"7",
 "dob(3i)"=>"18",
 "document"=>{"passportno"=>"bycucde63"}},
 "commit"=>"Create Employee"}
我已经浏览了几乎所有关于stackoverflow的帖子,这些帖子都是关于这个问题的,大部分都是关于

  • 不使用可访问的属性
  • 不使用
    接受
  • 不使用
    :文档属性
  • 如果我将
    config.active\u record.whitelist\u attributes
    的值更改为false,那么错误就会消失(开发者日志中有一个关于相同的警告),并且两个模型都会被创建,但当文档模型的属性为nil时,只有员工模型的属性会被传递的值填充。

    编辑#1 如果我试图将:document添加到attr_accessible,则会出现以下错误

    ActiveRecord::AssociationTypeMismatch in EmployeesController#create 
    

    我在这里做错了什么?

    我想,这里最简单的方法是将:document添加到attr\u accessible。 如果没有列出属性,ActiveRecord将不允许您“批量分配”它们

    关于我的评论,你会得出这样的结论:

    ...   
    "dob(3i)"=>"18",
    "document"=><#Document ...>}, # {"passportno"=>"bycucde63"} is just a Hash, not a Document, that's why it raises "ActiveRecord::AssociationTypeMismatch"
    "commit"=>"Create Employee"}
    

    我想,这里最简单的方法是将:document添加到attr\u accessible。 如果没有列出属性,ActiveRecord将不允许您“批量分配”它们

    关于我的评论,你会得出这样的结论:

    ...   
    "dob(3i)"=>"18",
    "document"=><#Document ...>}, # {"passportno"=>"bycucde63"} is just a Hash, not a Document, that's why it raises "ActiveRecord::AssociationTypeMismatch"
    "commit"=>"Create Employee"}
    

    我想,这里最简单的方法是将:document添加到attr\u accessible。 如果没有列出属性,ActiveRecord将不允许您“批量分配”它们

    关于我的评论,你会得出这样的结论:

    ...   
    "dob(3i)"=>"18",
    "document"=><#Document ...>}, # {"passportno"=>"bycucde63"} is just a Hash, not a Document, that's why it raises "ActiveRecord::AssociationTypeMismatch"
    "commit"=>"Create Employee"}
    

    我想,这里最简单的方法是将:document添加到attr\u accessible。 如果没有列出属性,ActiveRecord将不允许您“批量分配”它们

    关于我的评论,你会得出这样的结论:

    ...   
    "dob(3i)"=>"18",
    "document"=><#Document ...>}, # {"passportno"=>"bycucde63"} is just a Hash, not a Document, that's why it raises "ActiveRecord::AssociationTypeMismatch"
    "commit"=>"Create Employee"}
    

    理解质量分配

    Mass Assignment是Rails为使用参数散列构造对象的行为命名的名称。它是“质量分配”,即通过单个分配运算符为属性分配多个值

    以下代码段对Post模型的“名称”和“主题”属性执行大量赋值:

    Post.new(:name => "John", :topic => "Something")
    
    Post.create(:name => "John", :topic => "Something")
    
    Post.update_attributes(:name => "John", :topic => "Something")
    
    为了使其正常工作,您的模型必须允许为正在传递的哈希中的每个属性分配大量的属性

    有两种情况会导致失败:

    您有一个可访问的
    attr\u
    声明,其中不包括
    :name

    您有一个受保护的
    attr\u
    ,其中包括
    :name


    最近的默认情况是,属性必须通过可访问的属性手动白名单,才能成功进行批量分配。在此之前,默认情况下属性是可分配的,除非它们被明确黑名单属性保护,或者任何其他属性被白名单属性允许。

    理解批量分配

    Mass Assignment是Rails为使用参数散列构造对象的行为命名的名称。它是“质量分配”,即通过单个分配运算符为属性分配多个值

    以下代码段对Post模型的“名称”和“主题”属性执行大量赋值:

    Post.new(:name => "John", :topic => "Something")
    
    Post.create(:name => "John", :topic => "Something")
    
    Post.update_attributes(:name => "John", :topic => "Something")
    
    为了使其正常工作,您的模型必须允许为正在传递的哈希中的每个属性分配大量的属性

    有两种情况会导致失败:

    您有一个可访问的
    attr\u
    声明,其中不包括
    :name

    您有一个受保护的
    attr\u
    ,其中包括
    :name


    最近的默认情况是,属性必须通过可访问的属性手动白名单,才能成功进行批量分配。在此之前,默认情况下属性是可分配的,除非它们被明确黑名单属性保护,或者任何其他属性被白名单属性允许。

    理解批量分配

    Mass Assignment是Rails为使用参数散列构造对象的行为命名的名称。它是“质量分配”,即通过单个分配运算符为属性分配多个值

    以下代码段对Post模型的“名称”和“主题”属性执行大量赋值:

    Post.new(:name => "John", :topic => "Something")
    
    Post.create(:name => "John", :topic => "Something")
    
    Post.update_attributes(:name => "John", :topic => "Something")
    
    为了使其正常工作,您的模型必须允许为正在传递的哈希中的每个属性分配大量的属性

    有两种情况会导致失败:

    您有一个可访问的
    attr\u
    声明,其中不包括
    :name

    您有一个受保护的
    attr\u
    ,其中包括
    :name


    最近的默认情况是,属性必须通过可访问的属性手动白名单,才能成功进行批量分配。在此之前,默认情况下属性是可分配的,除非它们被明确黑名单属性保护,或者任何其他属性被白名单属性允许。

    理解批量分配

    Mass Assignment是Rails为使用参数散列构造对象的行为命名的名称。它是“质量分配”,即通过单个分配运算符为属性分配多个值

    以下代码段对Post模型的“名称”和“主题”属性执行大量赋值:

    Post.new(:name => "John", :topic => "Something")
    
    Post.create(:name => "John", :topic => "Something")
    
    Post.update_attributes(:name => "John", :topic => "Something")
    
    为了使其正常工作,您的模型必须允许为正在传递的哈希中的每个属性分配大量的属性

    有两种情况会导致失败:

    您有一个可访问的
    attr\u
    声明,其中不包括
    :name

    您有一个受保护的
    attr\u
    ,其中包括