首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails 5,从用户中删除角色

Rails 5,从用户中删除角色
EN

Stack Overflow用户
提问于 2016-10-25 00:05:06
回答 1查看 1.1K关注 0票数 3

我真的很难理解一些对于用rails编写代码至关重要的东西。我不知道问一个更基本的问题是什么,但我似乎一再遇到类似的问题。

我已经设法在我的Rails 5应用程序中安装了rolify。我使用rolify为用户分配角色。

现在,我正在尝试设置一个函数,以便在用户被分配之后从他们中删除角色。

在我的用户索引中,我有;

代码语言:javascript
运行
复制
<% user.roles.each do |role| %>
  <%= role.name.titleize %> <br>
<% end %>  

它显示了已分配的角色。

然后,我想补充一下:

代码语言:javascript
运行
复制
<%= link_to 'Destroy', role, method: :delete, data: { confirm: 'Are you sure?' } %>

我的销毁方法在我的assign_roles控制器中定义。它包括:

代码语言:javascript
运行
复制
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

这些路线是:

代码语言:javascript
运行
复制
resources :users, shallow: true do
    scope module: :users do
      resources :assign_roles
    end

没有assign_role.rb模型。我只是用一个控制器和一个视图。

当我试图在我的用户索引中使用破坏角色链接时,我会得到一个错误,上面写着:

代码语言:javascript
运行
复制
Couldn't find User with 'id'=

有人能看到我需要做些什么才能让assign_roles#destroy操作起作用吗?

我在assign_roles控制器中的创建操作可以工作。它包括:

代码语言:javascript
运行
复制
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

整个分配角色控制器:

代码语言:javascript
运行
复制
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

--我可以在控制台上做我想做的事情--我不知道如何从代码中完成它

在控制台中,我可以写:

代码语言:javascript
运行
复制
u.remove_role :fff, Organisation.first

然后成功地移除角色。

这是一个测试,但在代码中,我尝试使用current_user.organisation而不是Organisation.first。我的目标是允许用户仅在自己的组织中管理角色。

在我的分配角色控制器中,我复制了上面的破坏操作。

在“我的用户”索引中,我试图从用户中删除指定的角色如下:

代码语言:javascript
运行
复制
 <% 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>

错误说:

代码语言:javascript
运行
复制
undefined method `role_path' for #<#

我想知道我是否需要在路径中给它其他的东西,以便它能够找到赋值roles#destroy操作?

当我分配rake路由\ grep时,可以看到:

代码语言:javascript
运行
复制
 DELETE   /assign_roles/:id(.:format)                                             users/assign_roles#destroy

我尝试将用户索引中的路径更改为:

代码语言:javascript
运行
复制
<% 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 %>

但这就产生了这样的错误:

代码语言:javascript
运行
复制
Couldn't find User with 'id'=

我不明白这个错误意味着什么。

另一条线索

对,所以我可以看到问题出在哪里。我想不出怎么解决它了。

在控制台中,我可以以我想要的方式成功地删除用户的角色,方法是:

代码语言:javascript
运行
复制
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

在代码中,我当前从“分配角色”控制器执行的销毁操作如下:

代码语言:javascript
运行
复制
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

在日志中显示此过程:

代码语言:javascript
运行
复制
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控制器中尝试破坏操作的新尝试有:

代码语言:javascript
运行
复制
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

我认为有问题的是这句话:

代码语言:javascript
运行
复制
assigned_role = user.roles

它应该是我试图摧毁的特定角色(从用户分配的角色数组中)。

日志显示:

代码语言:javascript
运行
复制
[["user_id", 4], ["name", "#<Role::ActiveRecord_Associations_CollectionProxy:0x007f887ddb9c98>"], ["resource_type", "Organisation"], ["resource_id", 1]]

角色不应该是数组。它应该是一个特定的角色。因此,我的下一个猜测是尝试将角色添加到用户索引中的链接中,现在是:

代码语言:javascript
运行
复制
<%= link_to 'Remove role', assign_role_path(user, role),  method: :delete, data: { confirm: 'Are you sure?' } %>

然后,我需要关于如何在销毁操作中重写assigned_role行的想法,以便它知道我试图从用户那里取消分配哪个角色。

我对如何做到这一点没有什么好主意。

代码语言:javascript
运行
复制
# role = AppRole.find(params[:roles])
assigned_role = user.role
# user_roles = user.roles

有什么想法可以推动这个想法吗?

EN

回答 1

Stack Overflow用户

发布于 2016-11-08 14:49:59

你只想为一个用户销毁这个角色,对吗?所以

代码语言:javascript
运行
复制
params[:users]

不应该是复数的,并且:用户不是对参数有效的密钥。

你基本上是在尝试

代码语言:javascript
运行
复制
User.find(nil)

但却失败了

代码语言:javascript
运行
复制
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=

你也许应该用

代码语言:javascript
运行
复制
User.find(params[:id])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40229414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档