使用Devise选择用户的子节点

时间:2011-07-13 10:06:04

标签: ruby-on-rails devise simple-form mass-assignment

我有一个非常简单的问题:

用户模型:

class User < ActiveRecord::Base
   devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable 

  attr_accessible :id, :email, :password, :password_confirmation, :remember_me,
    :firstname, :lastname, :mobile_phone, :user_type, :department_id, :department_attributes

  belongs_to :department

  accepts_nested_attributes_for :department, :allow_destroy => false

部门模型:

class Department < ActiveRecord::Base

  has_many :users

  accepts_nested_attributes_for :users, :allow_destroy => true

我创建了一个表单,可以使用simple_form从现有用户中选择我的部门成员:

<%= simple_form_for @department, :validate => true  do |form| %>

    <%= form.error_messages %>
    <%= form.association :users, :prompt => 'assign a user', :label => 'User'%>
    <%= form.button :submit %>

<% end %>

然后我(尝试)通过部门控制器更新我的用户:

  def update
    @department = Department.find(params[:id])

    respond_to do |format|
      if @department.update_attributes(params[:department])
      ...

这会产生以下错误:

WARNING: Can't mass-assign protected attributes: user_ids

我的猜测是,某些设计设置会产生此错误但我不知道哪些设置。

你能帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:2)

attr_accessible :user_ids添加到您的部门模型。