Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-4装置和x2B;不能隐藏编辑和销毁链接_Ruby On Rails_Devise_Authorization_Ruby On Rails 4_Cancan - Fatal编程技术网

Ruby on rails Rails-4装置和x2B;不能隐藏编辑和销毁链接

Ruby on rails Rails-4装置和x2B;不能隐藏编辑和销毁链接,ruby-on-rails,devise,authorization,ruby-on-rails-4,cancan,Ruby On Rails,Devise,Authorization,Ruby On Rails 4,Cancan,我正在使用CanCan和Desive,但Desive用户是通过自定义UsersController管理的。我只希望登录用户看到编辑和销毁链接。但现在取决于传递给能力类的内容,它要么隐藏所有用户(包括登录用户)的编辑和销毁链接,要么将所有用户的编辑和销毁链接公开给任何登录用户,因此,如果不是他们的,他们可以编辑任何用户帐户事件 users/index.html.erb <% @users.each do |user| %> <div class="col-lg-3 co

我正在使用CanCan和Desive,但Desive用户是通过自定义UsersController管理的。我只希望登录用户看到编辑销毁链接。但现在取决于传递给能力类的内容,它要么隐藏所有用户(包括登录用户)的编辑销毁链接,要么将所有用户的编辑销毁链接公开给任何登录用户,因此,如果不是他们的,他们可以编辑任何用户帐户事件

users/index.html.erb

 <% @users.each do |user| %>
    <div class="col-lg-3 col-sm-4 col-6">
       <div><%= user.email %></div>

       <% if user_signed_in? %> 

         <% if can? :update, user %>
           <div class=" btn "><%= link_to "Edit", edit_user_path(user) %> </div>
         <% end %>

         <% if can? :destroy, user %>
          <div class="btn btn-danger"><%= link_to 'Remove', user, method: :delete, data: {confirmation: 'Are you sure'} %></div>
         <% end %>

      <div><%= link_to "Sign Out", destroy_user_session_path, method: :delete  %> </div>

      <% end %> <!-- closes user_signed_in? -->
   </div>
 <% end %>
请注意,我在第二个屏幕截图下方添加了Userscontroller

屏幕截图1显示,如果能力类中的user.id==user.id未注释,它公开了所有用户的销毁编辑链接,包括尚未登录的用户,登录的用户可以编辑不属于自己的帐户。在屏幕截图中,用户电子邮件中的真实签名是a@test.com但是您可以看到,对于未登录的用户,他可以访问编辑销毁链接,其中b@test.com

屏幕截图2,是我们在能力类中取消注释user==:user_id时得到的结果。“编辑”和“销毁”链接即使对已登录的用户也是隐藏的。

用户控制器的缩短版本

  class UsersController < ApplicationController
     before_action :set_user, only: [:show, :update, :destroy]
     before_filter :authenticate_user!, except: [:new, :create]
     load_and_authorize_resource , only: [:index, :edit, :destroy]

     respond_to :html, :json

    def index
      @users = User.all
      respond_with @users
    end

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

    def destroy
      @user.destroy
      redirect_to users_path, {notice: 'A user was removed'}
    end

    private

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

    def user_params
      params.require(:user).permit(:email, :password, :password_confirmation)
    end

  end
 class UsersSessionsController < Devise::SessionsController
    respond_to :html, :json

    def create
      super
      if signed_in?(resource) 

        #call the code to transfer guest cart to signed_in user
        load_current_cart
      end
    end
  end
class UsersController
会话控制器

  class UsersController < ApplicationController
     before_action :set_user, only: [:show, :update, :destroy]
     before_filter :authenticate_user!, except: [:new, :create]
     load_and_authorize_resource , only: [:index, :edit, :destroy]

     respond_to :html, :json

    def index
      @users = User.all
      respond_with @users
    end

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

    def destroy
      @user.destroy
      redirect_to users_path, {notice: 'A user was removed'}
    end

    private

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

    def user_params
      params.require(:user).permit(:email, :password, :password_confirmation)
    end

  end
 class UsersSessionsController < Devise::SessionsController
    respond_to :html, :json

    def create
      super
      if signed_in?(resource) 

        #call the code to transfer guest cart to signed_in user
        load_current_cart
      end
    end
  end
类用户会话控制器
由下面@gotva的答案修复。因此,**can:manage,User,:id=>User.ida@test.com谁是登录用户查看编辑销毁链接的用户。新截图:


尽你所能。rb:你有没有试过:

user ||= User.new # guest user
# you can here try some condition if you have multi user types
can [:new, :create], :all

在你的能力范围内。rb:你是否尝试过:

user ||= User.new # guest user
# you can here try some condition if you have multi user types
can [:new, :create], :all

尝试通过散列(而不是块)定义能力


尝试通过散列(而不是块)定义能力


谢谢这是我昨天第一次尝试的,它不起作用,也没有抛出任何错误。我决定再试一次,因为您将其作为答案发布,但它现在抛出错误:Users#index中的NoMethodError和#的未定义方法'user_id'。虽然用户已登录。谢谢。这是我昨天第一次尝试的,它不起作用,也没有抛出任何错误。我决定再试一次,因为您将其作为答案发布,但它现在抛出错误:Users#index中的NoMethodError和#的未定义方法'user_id'。虽然用户已登录。感谢您的建议答案,我已尝试过,但没有解决问题。感谢您的建议答案,我已尝试过,但没有解决问题。