0

尝试使用嵌套属性在一个表单中创建一个Organization和一个。User我收到以下错误:无法批量分配受保护的属性:用户(ActiveModel::MassAssignmentSecurity::Error)

把我的头发拉出来

组织.rb

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

user.rb(使用设计)

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

新的.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 回答