Rails 4 rolify和活动管理员无法添加/更新角色

时间:2014-09-28 05:46:31

标签: ruby-on-rails activeadmin rolify

您好我有两个表Active管理员和用户。我希望activeadmin能够创建用户或/和更改用户的角色。

我可以创建用户,但无法在创建和更新时向用户添加角色 您可以在底部看到错误。 Rails日志说未经许可的param roles_id

下面是我的管理员/用户,上次更改了

我正在使用Devise,CanCan& Rolify。 当我添加或更新角色时,会创建名为#的角色记录 它被添加到角色而不是选定的选项。我看到roles_id是不允许的params。 但我已将其添加到允许的参数中。即使在控制器中添加允许的参数

,它也不起作用
ActiveAdmin.register User do
  permit_params :email , :role_ids ,:password

  form do |f|
    f.inputs "Users" do
      f.input :email
      f.input :password
      f.input :roles, as: :select, multiple: false, collection: Role.all

    end
    f.actions
  end 

  show do
    binding.pry
    attributes_table do
      row :email
      row :roles do | user |
        user.roles.first.name unless user.roles.blank?
      end
    end
  end

  index do
    column :email
    column :roles do | user |
        user.roles.first.name unless user.roles.blank?
    end
    actions
  end
  controller do
    def create
      @user = User.new(user_params)
      add_roles(@user)
      create!
    end

    def update
      add_roles(resource)
      update!(user_params)
    end

    private
      def add_roles(resource)
        resource.roles = []
        params[:user][:role_ids].each { 
          |r| resource.add_role(Role.find(r)) unless r.blank? 
        }
      end
      def user_params
        params.require(:user).permit(:email, :password)
      end
    end
end

-log

Started POST "/admin/users" for 127.0.0.1 at 2014-09-28 11:10:52 +0530
Processing by Admin::UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"B+n639awLa6Oo1aijcuU0sCa1i3D7jV6qpMVfidjCls=", "user"=>{"email"=>"c@c.com", "password"=>"[FILTERED]", "role_ids"=>["", "2"]}, "commit"=>"Create User"}
Unpermitted parameters: role_ids
  ←[1m←[36mRole Load (0.0ms)←[0m  ←[1mSELECT  "roles".* FROM "roles"  WHERE "roles"."id" = ? LIMIT 1←[0m  [["id", 2]]
  ←[1m←[35mRole Load (0.0ms)←[0m  SELECT  "roles".* FROM "roles"  WHERE "roles"."name" = '#<Role:0x96fbf40>' AND "roles"."resource_type" IS NULL AND "roles"."resource_id" IS NULL  ORDER BY "roles"."id" ASC LIMIT 1
  ←[1m←[36m (0.0ms)←[0m  ←[1mbegin transaction←[0m
Binary data inserted for `string` type on column `name`
  ←[1m←[35mSQL (1.0ms)←[0m  INSERT INTO "roles" ("created_at", "name", "updated_at") VALUES (?, ?, ?)  [["created_at", "2014-09-28 05:40:52.858178"], ["name", "#<Role:0x96fbf40>"], ["updated_at", "2014-09-28 05:40:52.858178"]]
  ←[1m←[36m (45.0ms)←[0m  ←[1mcommit transaction←[0m
  ←[1m←[35mRole Load (1.0ms)←[0m  SELECT  "roles".* FROM "roles"  WHERE "roles"."id" = ? LIMIT 1  [["id", 22]]
  ←[1m←[36m (0.0ms)←[0m  ←[1mbegin transaction←[0m
  ←[1m←[35mUser Exists (0.0ms)←[0m  SELECT  1 AS one FROM "users"  WHERE "users"."email" = 'c@c.com' LIMIT 1
  ←[1m←[36mCACHE (0.0ms)←[0m  ←[1mSELECT  1 AS one FROM "users"  WHERE "users"."email" = 'c@c.com' LIMIT 1←[0m
  ←[1m←[35mUser Load (0.0ms)←[0m  SELECT  "users".* FROM "users"  WHERE "users"."authentication_token" = 's5bEQa9fiqX6teauQd_y'  ORDER BY "users"."id" ASC LIMIT 1
Binary data inserted for `string` type on column `encrypted_password`
  ←[1m←[36mSQL (1.0ms)←[0m  ←[1mINSERT INTO "users" ("authentication_token", "created_at", "email", "encrypted_password", "updated_at") VALUES (?, ?, ?, ?, ?)←[0m  [["authentication_token", "s5bEQa9fiqX6teauQd_y"], ["created_at", "2014-09-28 05:40:52.969185"], ["email", "c@c.com"], ["encrypted_passw
ord", "$2a$10$lcJpUKstBbKZ/VpNBBmzRedTB/c3ha2RqptMHjlLLjnrHEiUV0KPS"], ["updated_at", "2014-09-28 05:40:52.969185"]]
  ←[1m←[35mSQL (0.0ms)←[0m  INSERT INTO "users_roles" ("role_id", "user_id") VALUES (?, ?)  [["role_id", 22], ["user_id", 7]]
  ←[1m←[36m (15.0ms)←[0m  ←[1mcommit transaction←[0m
Redirected to http://localhost:3000/admin/users/7
Completed 302 Found in 379ms (ActiveRecord: 65.0ms)


Started GET "/admin/users/7" for 127.0.0.1 at 2014-09-28 11:10:53 +0530
Processing by Admin::UsersController#show as HTML
  Parameters: {"id"=>"7"}

1 个答案:

答案 0 :(得分:4)

尝试使用role_ids: []代替:role_ids

permit_params :email, :password, role_ids: []

permit_params :email, :password, :role_ids => []