我有链接访问玩家的load_player视图以访问属于current_user的玩家。那么我如何限制对没有归属俱乐部或俱乐部属于其他用户的current_user的load_player方法的访问。注意我想限制访问load_player页面。
routes.rb
get 'player/load_player/:id' => "player#load_player", as: :player
player_controller.rb
class PlayerController < ApplicationController
before_action :authenticate_user!
authorize_resource :class => false
layout "player"
def load_player
@club_hash = Club.where({:id => params[:id]}).pluck(:club_hash).first
end
end
ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.is?(:admin)
can :manage, :all
elsif user.is?(:owner)
can :read, all: { except: [UploadedSong]}
can :manage, Club, user_id: user.id
can :read, :load_player, id: user.id
elsif user.is?(:user)
can :read, all: { except: [UploadedSong]}
end
end
end
答案 0 :(得分:1)
current_user.clubs.where(id: params[:id]).pluck(:club_hash).try(:first)
# or add user_id inside where clause
Club.where(id: params[:id], user_id: current_user.id).pluck(:club_hash).try(:first)
希望这能解决你的问题:)