首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法删除用户

无法删除用户
EN

Stack Overflow用户
提问于 2013-05-23 23:27:02
回答 2查看 113关注 0票数 0

我陷入了非常奇怪的境地。我使用的是Devise和cancan,我不确定这两个人是否在我的工作中发挥作用。因此,我有一个具有父子关系的用户模型。

代码语言:javascript
复制
class User < ActiveRecord::Base
  has_many :child_users, :class_name => "User",:foreign_key => "parent_id", :dependent => :destroy
  belongs_to :parent, :class_name => "User"
end

当我删除子用户时,我可以成功地删除它,但是当我删除父用户时,我会被重定向到根页面,显示“您没有被授权访问此页面”。下面是我删除子用户时的代码

代码语言:javascript
复制
<h1>Users</h1>
<table class="table table-striped">
  <thead>
  <tr>
    <th>Email</th>
  </tr>
  </thead>
  <tbody>
  <% @users.each do |user| %>
      <tr>
        <td><%= user.email %></td>
        <td>
          <%= link_to 'Show', user_path(user), :class => 'btn btn-mini' %>
          <%= link_to 'Edit', edit_user_path(user), :class => 'btn btn-mini' %>
          <%= link_to 'Destroy', user_path(user), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
          <%= link_to "Categories", {:controller => "levels", :id => user.id },:class => 'btn btn-mini'%>
        </td>
      </tr>
  <% end %>
  </tbody>
</table>

<%= link_to "Add Sub Child", {:controller => "users", :action => "sub_child", :parent_id => current_user.id },:class => 'btn'%>

下面的代码是我删除父用户时使用的

代码语言:javascript
复制
<h1>User</h1>
<p></p>
<table width="40%">
  <tr>
    <td><b>Email</b></td>
    <td><%= @user.email %></td>
  </tr>
</table>

<div class="form-actions">
  <%= link_to 'Edit', edit_user_path(@user), :class => 'btn' %>
  <%= link_to 'Delete', user_path, :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-danger' %>
</div>

用户控制器

代码语言:javascript
复制
class UsersController < ApplicationController
  before_filter :authenticate_user!
  load_and_authorize_resource

  respond_to :html, :json, :xml

  def show
    @user = User.find(params[:id])
    respond_with @user
  end

  def new
    @user = User.new
    RAILS_DEFAULT_LOGGER.error '***** JLD Message *****'

  end

  def index
    respond_with(@users = current_user.child_users)
  end

  def edit
    @user = User.find(params[:id])
  end

  def create
   @user = User.new(params[:user])

    if @user.save
      flash[:notice] = "#{ @user.email } created."
      redirect_to users_path
    else
      render :action => 'new'
    end
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end

  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
end

class ApplicationController < ActionController::Base
  protect_from_forgery

  rescue_from CanCan::AccessDenied do |exception|
    redirect_to root_url, :alert => exception.message
  end
end

class Ability
  include CanCan::Ability

  def initialize(user)


    user ||= User.new # guest user (not logged in)
    # if user.admin?
    #  can :manage, :all
    #else

    can [:read, :update], User, :id => user.id
    can :manage, User, :parent_id => user.id

    #end

  end
end

任何建议都是非常感谢的。谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-24 01:34:58

我想通了。在它之前的能力模型中

代码语言:javascript
复制
can [:read, :update], User, :id => user.id 

现在有了这个变化

代码语言:javascript
复制
can :manage, User, :id => user.id 

我可以删除父用户。感谢胡安帕斯,当你问到能力模型时,它给了我方向。

票数 0
EN

Stack Overflow用户

发布于 2013-05-23 23:29:34

您在删除链接中忘记了@user

代码语言:javascript
复制
<%= link_to 'Delete', user_path(@user), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-danger' %>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16717961

复制
相关文章

相似问题

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