使用 Devise 选择用户的子级进行批量分配

发布于 2024-11-19 23:01:34 字数 1341 浏览 3 评论 0原文

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

用户模型:

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

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

你能帮忙吗?谢谢!

I have a very simple issue:

User model:

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

Departments model:

class Department < ActiveRecord::Base

  has_many :users

  accepts_nested_attributes_for :users, :allow_destroy => true

I created a form to be able to select my departments member from my existing users using 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 %>

Then I (try to) update my users via the department controller:

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

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

This generates following error:

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

My guess is that some devise settings generate this error but I don't know which ones.

Can you help? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2024-11-26 23:01:34

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

Add attr_accessible :user_ids to your Department model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文