rolify + activeAdmin按表单添加角色

时间:2013-12-14 15:57:01

标签: activeadmin formtastic rolify

我使用rolify + activeadmin gems。 我有2个资源:Staff和User(默认设计表)。 工作人员是一个映射唯一读表的模型,所以我不能在员工表中写。 我正在尝试使用主动管理员为使用has_one和belongs_to关联的用户添加角色:

class User < ActiveRecord::Base
  rolify
  belongs_to :staff
end

class Staff < ActiveRecord::Base
  has_one :user
end

在app / admin / staff.rb类我有这个:

    form do |f|
      f.inputs "Add role" do  |staff|
        f.input :roles,  :as => :select,      :collection => Role.global
      end
      f.actions
    end

So i want to add a role for a user using Staff admin resource.
when i click on submit form button i have this error:
NoMethodError in Admin/staffs#edit

Showing app/views/active_admin/resource/edit.html.arb where line #1 raised:

undefined method `roles' for #<Staff:0x00000005c6af70>
Extracted source (around line #1):

1: insert_tag renderer_for(:edit)

1 个答案:

答案 0 :(得分:2)

角色是用户模型的一部分,而不是员工模型。将表单添加到app/admin/user.rb,然后您就可以为用户分配角色。此外,在用户的表单中,您可以分配人员记录。以下是一个示例表单:

# app/admin/user.rb
form do |f|
  f.inputs 'Name' do
    f.input :name
  end

  f.inputs 'Add role'
    f.input :roles,  :as => :select, :collection => Role.global
  end

  f.inputs 'Staff' do
    f.input :staff
  end

  f.actions
end  

您还可以向员工添加委托,以便能够在Staff模型中原生地阅读角色。

# app/models/staff.rb
class Staff < ActiveRecord::Base
  attr_accessible :name, :user_id
  has_one :user
  delegate :roles, :to => :user
end
相关问题