【问题标题】:rolify+activeAdmin add role by formrolify+activeAdmin 通过表单添加角色
【发布时间】:2014-01-06 04:44:37
【问题描述】:

我正在使用 rolify+activeadmin gems。 我有 2 个资源:员工和用户(默认设计表)。 员工是一个映射只读表的模型,所以我不能在员工表中写入。 我正在尝试与活动管理员一起使用 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)

【问题讨论】:

    标签: activeadmin formtastic rolify


    【解决方案1】:

    角色是用户模型的一部分,而不是员工模型的一部分。将您的表单添加到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  
    

    您还可以向员工添加一个委托,以便能够在员工模型中本地读取角色。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多