1

我正在尝试设置对嵌套资源(专业)的访问权限。“.../companies/:id/specialties”。访问不属于我的公司工作正常。但我无法进入我的专长。请帮助我,因为我花了 4 个小时搜索解决方案,但没有任何结果。我有以下内容:

康康 1.6.9

//routes.rb
  resources :companies do
    resources :specialties
  end

//能力.rb

class Ability
  include CanCan::Ability

def initialize(user)
  user ||= User.new # guest user (not logged in)
  if user.super_admin?
    can :open, :admin_pages
  else
    cannot :open, :admin_pages
  end

  can [:edit, :update, :destroy], Company do |company|
    company.try(:admin) == user
  end

  can :manage, Specialty 
  end
end

//company_controller.rb

class CompaniesController < ApplicationController
  load_and_authorize_resource
  def new
    @company = current_user.build_company
  end

  def create
    @company = current_user.build_company params[:company]
    if @company.save
      redirect_to root_path, notice: I18n.t('notices.company_successfully_created')
    else
      render :new
    end
  end

  def edit
    @company = Company.find params[:id]
  end

  def update
    @company = current_user.company
    if @company.update_attributes(params[:company])
      redirect_to root_path, notice: I18n.t('notices.company_successfully_updated')
    else
      render action: 'edit'
    end
  end

end

//specialties_controller.rb

class SpecialtiesController < ApplicationController
  load_and_authorize_resource :company
  load_and_authorize_resource  through: :company

  before_filter :company, except: [:destroy]

  def index
    @specialties = @company.specialties
    respond_to do |format|
      format.json { 
        resource = params[:resource_type]=='user' ? User.new : Profile.new 
        render :json => {:success => true, :html => (render_to_string '_specialties_list.html.slim', :locals => {:resource => resource})} 
      }
      format.html {}
    end
  end

  def new
    @specialty = @company.specialties.build
  end

  def create
    @specialty = @company.specialties.build params[:specialty]
    if @specialty.save
      redirect_to company_specialties_path, notice: I18n.t('notices.specialty_successfully_created')
    else
      render :new
    end
  end

  def show
    @specialty = Specialty.find params[:id]
  end

  def edit
    @specialty = Specialty.find params[:id]
  end

  def update
    @specialty = Specialty.find params[:id]
    if @specialty.update_attributes(params[:specialty])
      redirect_to company_specialties_path, notice: I18n.t('notices.specialty_successfully_updated')
    else
      render action: 'edit'
    end
  end

  def destroy
    @specialty = Specialty.find(params[:id])
    @specialty.destroy
    redirect_to company_specialties_path
  end

  private

    def company
      @company = Company.find(params[:company_id])
    end

end
4

1 回答 1

0

在您的能力文件中,您仅授予公司管理员对同一公司的 [:edit, :update, :destroy] 访问权限。

当他尝试访问专业控制器中的任何操作时,第一个 load_and_authorize_call :company 将尝试读取公司。阅读公司后,会通过公司找到该专业,并检查用户是否对该专业有特定动作的权限。

在这种情况下,用户拥有专业的所有权限,但没有公司的读取权限,所以问题

因此,添加 :read 权限或使用 :manage 将所有权限授予公司管理员。

can [:read, :edit, :update, :destroy], Company do |company|
  company.try(:admin) == user
end
于 2013-04-11T09:45:18.650 回答