使用ActiveAdmin时从Rolify中选择角色

时间:2013-12-06 18:34:58

标签: ruby-on-rails-4 activeadmin cancan rolify

将Rails 4与ActiveAdmin 5以及CanCan和Rolify一起使用,我希望能够选择(通过复选框列表或其他内容)在通过活动管理界面编辑或创建新用户时适用于每个用户的角色。

这篇文章How to use ActiveAdmin on models using has_many through association?显示了构建表单的方法。虽然我收到错误:

undefined method `new_record?' for nil:NilClass

在执行.has_many

时,只需访问form do |f|属性即可
f.has_many :roles do |app_f|
  #app_f.inputs "Roles" do
    #if !app_f.object.nil?
      # show the destroy checkbox only if it is an existing appointment
      # else, there's already dynamic JS to add / remove new appointments
   #app_f.input :_destroy, :as => :boolean, :label => "Destroy?"
  #row app_f.role.name
  #end

  #  app_f.input :roles # it should automatically generate a drop-down select to choose from your existing patients

  #end
end

1 个答案:

答案 0 :(得分:0)

我发现通过f.input

采用不同的路径来访问角色效果更好

以下是工作代码:

  form do |f|
    f.inputs "Admin Details" do
      f.input :email
      f.input :first_name
      f.input :last_name
      f.input :alias
      f.input :bio
      f.input :password
      f.input :password_confirmation
      f.input :roles, :as => :check_boxes
    end
    f.actions
  end