0

我正在通过设计进行注册过程

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

<!-- 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

我还尝试将其添加到用户模型中:

  def after_initialize
    self.payments.build if self.payments.blank?
  end

为什么这些参数没有被保存有什么想法或建议?

4

1 回答 1

0

您正在尝试分配付款而不是付款属性。如果这是由 fields_for 在视图中返回的,则很可能是由于accepts_nested_attributes_for :payments您的 User 模型内部缺少。

更新:

您错过了传递给 的协会名称中的“s” fields_for。应该

fields_for :payments  do |payment|
于 2013-11-14T09:56:32.160 回答