0

ネストされた属性を使用して、1つのフォームでOrganizationとを作成しようとしています。User次のエラーが発生します:保護された属性を一括割り当てできません:user(ActiveModel :: MassAssignmentSecurity :: Error)

これで髪を抜く

Organisation.rb

class Organization < ActiveRecord::Base  
  attr_accessible :name, :users_attributes
  has_many :users, dependent: :destroy      
  accepts_nested_attributes_for :users  
end

user.rb(deviseを使用)

class User < ActiveRecord::Base
  attr_accessible :email     
  belongs_to :organization
end

new.html.haml

= form_for @organization do |f|

  = f.label :name, "Company Name"
  = f.text_field :name, placeholder: "Company Name"

  = f.fields_for :user do |ff| -# tried :users here and the form doesn't render
    = ff.label :email, "Email Address"
    = ff.email_field :email, placeholder: "Email Address"

= f.submit "Create Account"
4

1 に答える 1

1

これはStackoverflowで多くの回答があります。

f.fields_for :users

また、コントローラーでは、ユーザーを作成する必要があります。

@organization.users.build

Can't mass-assign protected attributes: user存在しないため、ユーザー属性にアクセスできないために取得します。

于 2013-01-20T23:01:16.020 に答える