Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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 由于质量分配限制,Rails脚手架参照无法保存_Ruby On Rails_Crud_Scaffolding_Sqlite3 Ruby - Fatal编程技术网

Ruby on rails 由于质量分配限制,Rails脚手架参照无法保存

Ruby on rails 由于质量分配限制,Rails脚手架参照无法保存,ruby-on-rails,crud,scaffolding,sqlite3-ruby,Ruby On Rails,Crud,Scaffolding,Sqlite3 Ruby,我是rails的新手,所以如果这是一个愚蠢的问题,请原谅我——我根本无法拼凑出足够的答案来判断我是否开始错误。来吧 我用rails g scaffold Company name:string description:text location\u city:string location\u state:string accounttype:references创建了与我的公司模型相关的所有内容 我用rails g scaffold Accounttype id:primary\u key n

我是rails的新手,所以如果这是一个愚蠢的问题,请原谅我——我根本无法拼凑出足够的答案来判断我是否开始错误。来吧

  • 我用
    rails g scaffold Company name:string description:text location\u city:string location\u state:string accounttype:references创建了与我的
    公司
    模型相关的所有内容

  • 我用
    rails g scaffold Accounttype id:primary\u key name:string price:decimal创建了与
    Accounttype
    模型相关的所有内容

  • 因此,我的
    公司
    车型包含以下津贴:

    属于:accounttype

    attr\u可访问:描述,:位置城市,:位置州,:名称

  • 当我转到脚手架生成的公司编辑表单,并在这些字段中输入数据时,它会抛出一个错误:
    无法批量分配受保护的属性:accounttype

  • 如果我将
    :accounttype
    添加到
    attr\u accessible
    ,它将抛出
    accounttype(#7017525424200)预期的、带有以下请求参数的get字符串(#70175215042700)

    {“utf8”=>“✓",
    “真实性令牌”=>“oXm3cqo0jemKYFB5OGqn5ixXLSB+bH19/1RHPUU0ZU=”,
    “公司”=>{“名称”=>“Acme公司”,
    “说明”=>“a”,
    “地点城市”=>“圣地亚哥”,
    “地点州”=>“加利福尼亚州”,
    “accounttype”=>“1”},
    “提交”=>“创建公司”}


  • 那么,我使用
    :references
    链接这两个模型是不是有问题?如果我使用它没问题,那么我应该怎么做才能让创建/更新工作正常进行?

    您应该研究资源嵌套和嵌套资源的使用

    accept_attributes_for :accounttype
    

    祝贺您使用了learning rails!您必须在
    公司的
    模型中为
    使用
    接受嵌套的属性,以便将属性直接分配给
    Accounttype
    模型。如下所示:

    belongs_to :accounttype
    
    accepts_nested_attributes_for :accounttype
    
    attr_accessible :description, :location_city, :location_state, :name, :accounttypes_attributes
    
    请注意,由于您使用的是attr\u accessible,因此必须添加
    accounttypes\u属性
    此外,我建议将
    Accounttype
    更改为
    Accounttype


    来源:

    谢谢jason给出的完整答案。然而,即使做出了这些更改,它仍然会抛出相同的错误:
    无法批量分配受保护的属性:accounttype
    谢谢@pavel给出的答案,你是不是想省去“嵌套”属性“从那以后?@Kristian您还应该检查您的参数是否具有accounttype的属性,例如
    params[:company][:accounttype\u name]