[英]Rails 5 - Rolify - trying to remove an instance of a role from a user
我試圖學習如何使用我的Rails 5應用程序rolify。
我以前問了很多關於Rolify的問題,最近才在這里,但是我找不到幫助。
我努力想弄清楚如何從用戶中刪除指定的角色。
我的架構中的表具有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
該錯誤在我的分配角色控制器的銷毀操作中抱怨此公式(這是某人對另一個SO帖子的建議的副本):
role = params[:user][:roles]
當我嘗試將destroy動作作為:
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
我有一個關於codementor的會議,最后得到了答案。
答案是:
def destroy
#/assign_roles/9?user_id=27
user = User.find_by_id(params[:user_id])
assigned_role = Role.find(params[:id])
organisation = Organisation.find(current_user.organisation)
user.remove_role assigned_role.name.to_sym, current_user.organisation #user.organisation
flash[:notice] = "role deleted"
redirect_to root_path
end
在視圖中:
<td><%= link_to 'Remove role', assign_role_path(role, user_id: user), method: :delete, data: { confirm: 'Are you sure?' } %></td>
具體問題是角色名稱在被刪除之前需要轉換為字符串,並且用戶查找方法正在最近的資源中搜索(這意味着它在角色模型中尋找id,而不是用戶模型。這意味着id將無法返回用戶ID。我不理解這部分rails的搜索結構 - 所以我必須把它當作給定(不理解它),但非常高興,經過多年努力解決這個問題,我可以繼續前進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.