【问题标题】:Rails 4 rolify and active admin unable to add / update rolesRails 4 rolify 和活动管理员无法添加/更新角色
【发布时间】:2014-09-28 05:46:31
【问题描述】:

您好,我有两个表 Active admin 和 user。我希望 activeadmin 能够创建用户或/并更改用户的角色。

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

下面是我最后一次更改的管理员/用户

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

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

-日志

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"}

【问题讨论】:

    标签: ruby-on-rails activeadmin rolify


    【解决方案1】:

    尝试role_ids: [] 而不是:role_ids

    permit_params :email, :password, role_ids: []

    permit_params :email, :password, :role_ids =&gt; []

    【讨论】:

    • 您好,感谢您的回答,但我在遵循您的回答语法错误时收到此错误,意外的 '\n',期待 tASSOC (SyntaxError)
    猜你喜欢
    • 1970-01-01
    • 2013-08-10
    • 2016-03-18
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2011-09-26
    • 2021-04-12
    • 1970-01-01
    相关资源
    最近更新 更多