Rails 3 UnknownAttributeError对于嵌套模型

时间:2012-09-28 19:00:34

标签: ruby-on-rails-3 devise nested-attributes simple-form

我没有特别的原因得到了UnkownAttributeError,我的模型似乎设置正确......

School.rb

class School < ActiveRecord::Base
  attr_protected :id, :created_at, :updated_at

  #relationships
  has_many :users

  accepts_nested_attributes_for :users
end

My School模型过去有以下内容,但它为用户字段产生了MassAssignmentSecurity错误:

attr_accessible :country, :name, :state_or_province, :users_attributes

User.rb

class User < ActiveRecord::Base

  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :instructor_id, :first_name, :last_name, :school_id

  #relationships
  belongs_to :school

end

new.html.haml

= simple_form_for @school do |f|
  .well
    = f.input :name, :as => :hidden
    = f.input :country, :as => :hidden
    = f.input :state_or_province, :as => :hidden
  .well
    = f.simple_fields_for @school.users.build do |user_form|
      = user_form.input :first_name, :required => true
      = user_form.input :last_name, :required => true
      = user_form.input :username, :required => true
      ...
  = f.button :submit, "Next"

注意:我的新操作正在填充@school来自上一页收集的会话信息,我正在制作一个多步骤表单。学校数据是完全有效的,如果我要删除用户表单,它可以毫无困难地保存学校。

我在创建操作中遇到的特定错误消息:

  

SchoolsController中的ActiveRecord :: UnknownAttributeError #create

     

未知属性:用户

发送的params看起来有点像这样:

  

{“school”=&gt; {“name”=&gt;“Elmwood Elementary”,“country”=&gt;“38”,   “state_or_province”=&gt;“448”,“user”=&gt; {“first_name”=&gt;“joe”,   “last_name”=&gt;“asdas”,“username”=&gt;“asasdads”,   “email”=&gt;“asdasd@sdas.ca”,“密码”=&gt;“[已过滤]”,   “password_confirmation”=&gt;“[过滤]”}},“提交”=&gt;“下一步”}

这可能是Devise或simple_form的错误吗?我正在使用Rails 3.2.3

1 个答案:

答案 0 :(得分:2)

好的,显然我需要提供符号:users - 关系的名称作为我工作的第一个参数。