나는 레일에 코드를 작성하는 데있어 기본적인 것을 이해하기 위해 정말로 애 쓰고있다. 나는 더 근본적인 질문을하는 것이 무엇인지 모르지만, 나는 비슷한 문제가 반복적으로 발생하는 것 같다.Rails 5, Rolify - 사용자로부터 역할 제거
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?' } %>
는 방법은 내 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 모델이 없습니다. 컨트롤러와 뷰를 사용합니다.
Couldn't find User with 'id'=
사람이 내가 # 작동하기위한 조치를 파괴 assign_roles를 얻기 위해 무엇을해야하는지 볼 수 있습니다
내 사용자 인덱스에 파괴하는 역할 링크를 사용하려고
, 나는 말한다 오류가?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
그러면 역할이 성공적으로 제거됩니다.
이것은 테스트로서 좋지만 코드에서는 Organisation.first 대신 current_user.organisation을 사용하려고합니다. 내 목표는 사용자가 자신의 조직에서만 역할을 관리 할 수있게하는 것입니다.
내 역할 컨트롤러에서 위의 복사 작업이 삭제되었습니다.내 사용자 지표에서
, 내가 같은 사용자의 할당 된 역할을 삭제하려는 시도가 :
<% 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 #<#
궁금 그렇지 않으면 경로에 할당 역할 # destroy 액션을 찾을 수 있습니까?
내가 갈퀴질 때 | 그렙 할당, 내가 볼 수
Couldn't find User with 'id'=
그렇게하지 :이 오류를 제공하는 다음
<% 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 %>
그러나 :
DELETE /assign_roles/:id(.:format) users/assign_roles#destroy
내가 내 사용자 지표의 경로를 변경 시도 이 오류의 의미를 이해하십시오.
또 다른 단서
오른쪽, 그래서 문제가 발생 어디서 볼 수 있습니다. 나는 그것을 고치는 방법에 대한 아이디어가 없다.
코드에서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
이 할당 역할 컨트롤러에서 내 현재의 파괴 행위라고하는 :
콘솔에서, 나는 성공적으로이 일을함으로써 내가 원하는 방식으로 사용자의 역할을하기 위해 제거 할 수 있습니다
을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]]
번째 파라미터는 "롬이다
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
로그에서이 프로세스를 나타낸다 르 ", 어쩌면 그것은 역할의 이름이어야한다 (나는 생각한다). 역할은 테이블 이름입니다.
이 작업을 수행하기 위해 플러그인하는 방법을 모르겠습니다. 콘솔에서 처리 할 수있는 방식으로 코드를 처리하는 방법을 알 수 있습니까?
다음 시도
assign_roles 컨트롤러에서 파괴 활동에서 나의 새로운 시도가 있습니다
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?' } %>
그런 다음, 나는 내 파괴 행동에 assigned_role 라인을 다시 할 수있는 방법에 대한 아이디어를 필요로하는 그래서 사용자로부터 할당을 취소하려는 역할을 알고 있습니다.
나는 그 방법에 대한 좋은 생각이 없습니다.
# role = AppRole.find(params[:roles])
assigned_role = user.role
# user_roles = user.roles
이 아이디어를 발전 시키는데 도움이되는 아이디어가 있습니까?
당신이'index' 방법을 제시해주십시오 수 사용해야합니다
실패
에 노력하고? – idej
로그는 무엇을 표시합니까? – Marcus
'user = User.find (params [: users]) '직전에'puts params'를 통해 params 해쉬를 출력합니다. 제 생각 엔'user = User.find (params [: id])'또는'user = User.find (params [: user_id])'를 사용해야합니다. – Magnuss