![](/img/trans.png)
[英]Rails 5 - Rolify - trying to remove an instance of a role from a user
[英]Rails 5, Rolify - removing role from user
我真的很難理解在rails中編寫代碼的基礎知識。 我不知道問一個更基本的問題是什么,但我似乎反復遇到類似的問題。
我已經設法在我的Rails 5應用程序中設置rolify。 我使用rolify為用戶分配角色。
現在,我正在嘗試設置一個功能,以便在分配用戶后刪除用戶的角色。
在我的用戶索引中,我有;
<% user.roles.each do |role| %>
<%= role.name.titleize %> <br>
<% end %>
這顯示了已分配的角色。
然后,我試圖添加:
<%= link_to 'Destroy', role, method: :delete, data: { confirm: 'Are you sure?' } %>
我的destroy方法在我的assign_roles控制器中定義。 它有:
def destroy
user = User.find(params[:users])
# role = AppRole.find(params[:roles])
assigned_role = user.roles
# user_roles = user.roles
# organisation = Organisation.first
# byebug
user.remove_role assigned_role.name, @current_user.organisation
flash[:notice] = "Successfully removed"
redirect_to action: :index
end
路線是:
resources :users, shallow: true do
scope module: :users do
resources :assign_roles
end
沒有assign_role.rb模型。 我只是使用控制器和視圖。
當我嘗試在我的用戶索引中使用destroy role鏈接時,我收到一條錯誤消息:
Couldn't find User with 'id'=
任何人都可以看到我需要做什么才能使assign_roles#destroy動作起作用?
我在assign_roles控制器中的創建操作有效。 它有:
def create
user = User.find(params[:users])
role = AppRole.find(params[:roles])
# organisation = Organisation.first
@organisation = Organisation.find(@current_user.organisation)
# byebug
user.add_role role.display_name, organisation
flash[:notice] = "Successfully created"
redirect_to action: :index
end
整個分配角色控制器:
class Users::AssignRolesController < ApplicationController
before_action :authenticate_user!
def index
# if @current_user.is_admin?
@app_roles = AppRole.all
# else
# @app_roles = AppRole.where(category: relevant_category)
# end
# if @current_user.is_admin?
@users = User.all
# else
# @users = current_user.organisation.users.select { |u| u.id != current_user.organisation.owner_id }
# end
end
def create
user = User.find(params[:users])
role = AppRole.find(params[:roles])
# organisation = Organisation.first
@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.joins(:profiles).where('profiles.organisation_id = ?' @current_user.organisation.id)
# @users = User.all
@current_user.organisation.users
end
def update
end
def destroy
user = User.find(params[:users])
# role = AppRole.find(params[:roles])
assigned_role = user.roles
# user_roles = user.roles
# organisation = Organisation.first
# byebug
user.remove_role assigned_role.name, @current_user.organisation
flash[:notice] = "Successfully created"
redirect_to action: :index
end
end
我可以做我想從控制台做什么 - 我不知道如何從代碼中做到這一點
在控制台中,我可以寫:
u.remove_role :fff, Organisation.first
然后成功刪除該角色。
這可以作為測試,但在代碼中我試圖使用current_user.organisation而不是Organisation.first。 我的目標是允許用戶僅管理自己組織中的角色。
在我的assign roles控制器中,我有上面復制的destroy動作。
在我的用戶索引中,我嘗試從用戶刪除指定的角色,如下所示:
<% user.roles.each do |role| %>
<table class="table table-bordered">
<tr>
<td><%= role.name.titleize %></td>
<td><%= link_to 'Remove role', role, method: :delete, data: { confirm: 'Are you sure?' } %></td>
錯誤說:
undefined method `role_path' for #<#
我想知道我是否需要在路徑中給它一些其他內容,以便它可以找到賦值角色#dumping action?
當我耙路線| grep assign,我可以看到:
DELETE /assign_roles/:id(.:format) users/assign_roles#destroy
我嘗試將用戶索引中的路徑更改為:
<% 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), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</table>
<% end %>
但后來出現了這個錯誤:
Couldn't find User with 'id'=
我不明白這個錯誤意味着什么。
另一個線索
是的,所以我可以看到問題出現的地方。 我不知道如何修復它。
在控制台中,我可以通過以下方式成功刪除用戶的角色:
u.remove_role :sdfddd, Organisation.first
Organisation Load (2.0ms) SELECT "organisations".* FROM "organisations" ORDER BY "organisations"."id" ASC LIMIT $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 "roles"."name" = $2 AND "roles"."resource_type" = $3 AND "roles"."resource_id" = $4 [["user_id", 4], ["name", "sdfddd"], ["resource_type", "Organisation"], ["resource_id", 1]]
(0.1ms) BEGIN
SQL (0.4ms) DELETE FROM "users_roles" WHERE "users_roles"."user_id" = $1 AND "users_roles"."role_id" = 6 [["user_id", 4]]
(1.6ms) COMMIT
在代碼中,我從assign角色控制器的當前銷毀操作,其中說:
def destroy
# user = User.find(params[:users])
user = User.find(params[:id])
# role = AppRole.find(params[:roles])
assigned_role = user.roles
# user_roles = user.roles
organisation = Organisation.first
# organisation = Organisation.find(current_user.organisation)
# byebug
user.remove_role assigned_role.name, organisation
flash[:notice] = "Successfully created"
redirect_to root_path
end
在日志中顯示此過程:
User Load (1.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 4], ["LIMIT", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]]
Organisation Load (0.7ms) SELECT "organisations".* FROM "organisations" ORDER BY "organisations"."id" ASC LIMIT $1 [["LIMIT", 1]]
Role Load (1.0ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND "roles"."name" = $2 AND "roles"."resource_type" = $3 AND "roles"."resource_id" = $4 [["user_id", 4], ["name", "Role"], ["resource_type", "Organisation"], ["resource_id", 1]]
第二個參數是“角色”,也許它應該是角色的名稱(我認為)。 角色是表名。
我不知道如何插入它來使這項工作。 任何人都可以看到我如何能夠以我在控制台中執行的方式處理代碼?
下一步嘗試
我對assign_roles控制器中的destroy動作的新嘗試有:
def destroy
# user = User.find(params[:users])
user = User.find(params[:id])
# role = AppRole.find(params[:roles])
assigned_role = user.roles
# user_roles = user.roles
# organisation = Organisation.first
organisation = Organisation.find(current_user.organisation)
# byebug
user.remove_role assigned_role, organisation
flash[:notice] = "Successfully created"
redirect_to root_path
end
我認為問題在於這一行:
assigned_role = user.roles
它應該是我試圖銷毀的特定角色(來自用戶的指定角色數組)。
日志顯示:
[["user_id", 4], ["name", "#<Role::ActiveRecord_Associations_CollectionProxy:0x007f887ddb9c98>"], ["resource_type", "Organisation"], ["resource_id", 1]]
該角色不應該是一個數組。 它應該是一個特定的角色。 所以,我的下一個猜測是嘗試將角色添加到我的用戶索引中的鏈接,現在是:
<%= link_to 'Remove role', assign_role_path(user, role), method: :delete, data: { confirm: 'Are you sure?' } %>
然后,我需要有關如何在destroy操作中重寫assigned_role行的想法,以便它知道我試圖從用戶取消分配的角色。
我對如何做到這一點沒有任何好的想法。
# role = AppRole.find(params[:roles])
assigned_role = user.role
# user_roles = user.roles
任何可能有助於推動這一思想的想法?
你想要破壞一個用戶的角色,對吧? 所以
params[:users]
不應該是復數,並且:用戶不是params的有效密鑰。
你基本上是想要
User.find(nil)
失敗了
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=
你可能應該使用
User.find(params[:id])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.