Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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 设计并激活模型::MassaSignmentSecurity::错误_Ruby On Rails_Ruby_Activerecord_Devise_Mass Assignment - Fatal编程技术网

Ruby on rails 设计并激活模型::MassaSignmentSecurity::错误

Ruby on rails 设计并激活模型::MassaSignmentSecurity::错误,ruby-on-rails,ruby,activerecord,devise,mass-assignment,Ruby On Rails,Ruby,Activerecord,Devise,Mass Assignment,我正在通过Desive进行注册过程 我试图将“付款”保存给通过公式注册的“用户”。 当用户从复选框中选择“银行”时,还应向用户创建一个依赖的付款 <!-- language: ruby --> class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackabl

我正在通过Desive进行注册过程

我试图将“付款”保存给通过公式注册的“用户”。 当用户从复选框中选择“银行”时,还应向用户创建一个依赖的付款

<!-- language: ruby -->
class User < ActiveRecord::Base

 devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name

  attr_accessible :payments_attributes
  # also tried attr_accessible :payments_attributes, :payments
  has_many :payments, :autosave => true

  accepts_nested_attributes_for :payments, allow_destroy: false

end


class Payment < ActiveRecord::Base

  attr_accessible :method, :paid
  attr_accessor :method, :paid

  belongs_to :user

end

new.html.erb

  <%= f.fields_for :payment do |payment| %>
        <div>
          <%= payment.radio_button(:method, 'bank') %>
          <%= payment.label :bank_transfer %>
          <%= payment.radio_button(:method, 'paypal') %>
          <%= payment.label :paypal %>
        </div>
    <% end %>



These are the attributes I wanna set:

my_attributes = {"first_name"=>"Max", "last_name"=>"Mustermann", "password"=>"12345678", "password_confirmation"=>"12345678", "email"=>"max@mustermann.at", "company"=>"ACME INC", "industry"=>{"title"=>"Metall", "oenace"=>"123"}, "payments"=>{"method"=>"bank_transfer", "paid"=>"false"}}

User.new(my_attributes)
# ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: payments

有没有关于为什么不保存这些参数的想法或建议?

您正在尝试分配付款而不是付款属性。如果这是由视图中的字段\u返回的,则很可能是由于用户模型中缺少
接受\u嵌套的\u属性\u:payments

更新:

您错过了传递给的
字段\u的关联名称中的's'。应该是

fields_for :payments  do |payment|

抱歉,复制/粘贴错误:我的模型集中有以下内容:
接受\u嵌套的\u属性\u:payments,allow\u destroy:false
那么您也可以添加表单代码吗
fields_for
没有达到您的预期效果,因此可能存在错误。hm当我更改:payment to payments:my Desive/registrations/new.html.erb时,f.fields_for的整个部分将不会呈现。因为您没有给定型号的付款。在您的控制器中添加类似于
@user.payments.build的内容
它需要是
payments\u属性:{}
,而不是
payment:{}
fields_for :payments  do |payment|