簡體   English   中英

CanCan與Rails有關的問題

[英]CanCan issues with Rails

我遇到了一個問題,我的Rails應用程序的所有者無法訪問使用CanCanCan gem進行權限控制的Map控制器創建和顯示方法。

這是我當前配置的能力模型:

class Ability
include CanCan::Ability

def initialize(user)

 if user.role.name == 'owner'

   can :manage, Map, venue_id: user.company.venues.pluck(:id)

 end
end

user.company.venues.pluck(:id)的輸出:

 Company Load (0.3ms)  SELECT  `companies`.* FROM `companies` WHERE `companies`.`id` = 4 LIMIT 1
   (0.4ms)  SELECT `venues`.`id` FROM `venues` WHERE `venues`.`company_id` = 4
 => [1] 

這是我的地圖控制器代碼:

class MapsController < ApplicationController
  before_action :authenticate_user!
  respond_to :html, :json
  load_and_authorize_resource

  def index
    respond_with(@maps)
  end

  def new
   respond_with(@maps)
  end

  def create
    @map = Map.create(map_params)
    if @map.save
      respond_to do |format|
        flash[:notice] = t('floors.created')
        format.json
      end
    else
      flash[:alert] = t('floors.error')
    end
  end

  def show
    respond_with(@map)
  end

  def edit

  end

  def update

  end

  def map_params
    params.require(:map).permit(:alias, :venue_id, :map)
  end

end

地圖屬於場所,並具有一個events_id。 我可以在自己的場所頁面的“部分地圖”中獲取“地圖”: 場地地圖

但是,當我嘗試創建地圖或轉到“地圖顯示”頁面時,出現“ CanCan訪問被拒絕”錯誤: 可以拒絕訪問

關於為什么CanCanCan允許我查看Maps索引但在創建或查看Map show控制器時訪問被拒絕的任何想法。

我不太確定您是#show動作,但絕對不應該這樣做:

can :manage, Map, venue_id: condition

原因是您的資源@map = Map.new沒有場所。

它要檢查:

@map.venue_id
=> nil
current_user.company.venues.pluck(:id)
=> [1]
@map.venue_id ==  current_user.company.venues.pluck(:id)
# aka     nil == [1]
=> false

請嘗試以下操作:

can [:new, :create], Map
can [:show, :edit, :update, :destroy], Map, venue_id: condition

嘗試在load_and_authorize_resource中明確聲明該類

load_and_authorize_resource class: "Map"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM