gpt4 book ai didi

ruby-on-rails - Rails 5 - Rolify - 试图从用户中删除角色实例

转载 作者:数据小太阳 更新时间:2023-10-29 08:02:46 28 4
gpt4 key购买 nike

我正在尝试学习如何在我的 Rails 5 应用程序中使用 rolify。

我之前问过很多关于 Rolify 的问题,最近一次是 here但我找不到帮助。

我正在努力尝试弄清楚如何从用户中删除分配的角色。

我的模式中的表有 app_roles 表(这是我的 CRUD,用于制作可以单独分配给用户的角色)、用户、角色和 user_roles - 如:

create_table "app_roles", force: :cascade do |t|
t.string "title"
t.string "display_name"
t.integer "category"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "failed_attempts", default: 0, null: false
t.string "unlock_token"
t.datetime "locked_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "organisation_id"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true, using: :btree
end

create_table "users_roles", id: false, force: :cascade do |t|
t.integer "user_id"
t.integer "role_id"
t.index ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id", using: :btree
end

create_table "roles", force: :cascade do |t|
t.string "name"
t.string "resource_type"
t.integer "resource_id"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id", using: :btree
t.index ["name"], name: "index_roles_on_name", using: :btree
end

关联是:

用户

rolify strict: true # strict means you get true only on a role that you manually add
attr_accessor :current_role

作用

has_and_belongs_to_many :users, :join_table => :users_roles

belongs_to :resource,
:polymorphic => true,
:optional => true

应用程序角色(没有关联 - 这是我用来通过表单创建新角色的 CRUD)

我的用户模型有:

class User < ApplicationRecord
rolify strict: true # strict means you get true only on a role that you manually add
attr_accessor :current_role

我不知道如何使用 rolify 来分配角色,就像它在 wiki 中显示的那样。这就是为什么我制作了一个单独的资源。这称为分配角色。我有一个 assign_roles_controller.rb,它有:

class Users::AssignRolesController < ApplicationController

before_action :authenticate_user!

def index
@app_roles = AppRole.all
end


def create
user = User.find(params[:users])
role = AppRole.find(params[:roles])
organisation = Organisation.find(current_user.organisation)
# byebug

user.add_role role.display_name, organisation

flash[:notice] = "Successfully created"
redirect_to action: :index
end

def show
@users = User.all
end

def update
end

def destroy

# user = User.find(params[:users])
# user = User.find(params[:id])
user = User.find_by_id(params[:id])
# User.find(params[:id])
# role = AppRole.find(params[:roles])
role = params[:user][:roles]
# assigned_role = user.roles
# user_roles = user.roles
# organisation = Organisation.first
organisation = Organisation.find(current_user.organisation)

# byebug

user.remove_role role.display_name, organisation

flash[:notice] = "Successfully created"
redirect_to root_path
end

end

路由是嵌套的,所以我有:

resources :users, shallow: true do
scope module: :users do
resources :assign_roles

在我的用户索引中,我试图显示一个用户列表,其中包含他们的每个角色和一个删除角色链接。在 users/index.html.erb 中,我有:

<% user.roles.each do |role| %>
<table class="table table-bordered">
<tr>
<td><%= role.name.titleize %></td>
<td><%= link_to 'Remove role', assign_role_path(user.roles), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</table>
<% end %>

在控制台中,我可以做:

Organisation.find_roles(nil, User.find_by(organisation_id:1))
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."organisation_id" = $1 LIMIT $2 [["organisation_id", 1], ["LIMIT", 1]]
Role Load (0.7ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (resource_type IN ('Organisation')) [["user_id", 43]]
=> #<ActiveRecord::AssociationRelation [#<Role id: 3, name: "asdf", resource_type: "Organisation", resource_id: 1, created_at: "2016-10-24 23:00:40", updated_at: "2016-10-24 23:00:40">, #<Role id: 4, name: "ffff", resource_type: "Organisation", resource_id: 1, created_at: "2016-11-09 00:26:56", updated_at: "2016-11-09 00:26:56">]>

用户有 2 个角色。

当我尝试删除其中一个时,我收到一条错误消息:

undefined method `[]' for nil:NilClass

该错误在我的分配角色 Controller 的销毁操作中提示这个公式(这是某人对另一个 SO 帖子的建议的副本):

role = params[:user][:roles]

当我尝试销毁操作时:

def destroy
user = User.find_by_id(params[:id])
assigned_role = user.roles
organisation = Organisation.find(current_user.organisation)

# byebug

user.remove_role assigned_role.display_name, organisation

flash[:notice] = "Successfully created"
redirect_to root_path
end

然后我收到一条错误消息:

undefined method `roles' for nil:NilClass

我认为该错误可能与我试图从用户中删除一个角色有关,但我使用复数来找到它们。我不确定如何找到我在使用索引中引用的特定角色。

谁能帮我解决这个问题?

最佳答案

请引用一次。

 def destroy
user = User.find_by_id(params[:id])
assigned_role = user.role_name
organisation = Organisation.find(current_user.organisation)

# byebug

user.remove_role assigned_role, organisation

flash[:notice] = "Successfully created"
redirect_to root_path
end

关于ruby-on-rails - Rails 5 - Rolify - 试图从用户中删除角色实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40796399/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com