2013-04-11 76 views
1

我试图设置访问嵌套资源(专业)前。 “... /公司/:ID /特产”。访问不属于我的公司工作正常。但我无法访问我的专业。请帮助我,因为我花了4个小时搜索解决方案而没有任何结果。 我有以下几点:Cancan错误,同时授权嵌套资源

惨惨1.6.9

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

//ability.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 

//companies_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 
+0

是否有任何特定操作会引发错误?什么是堆栈跟踪。如果您打算这样做,请使用pastebin粘贴堆栈跟踪并回复URL。 – manoj 2013-04-11 07:18:25

+0

例如(公司id = 3是我的)http:// localhost:3000/companies/3/specialities不能访问,而我可以访问http:// localhost:3000/companies/3/edit和I不能访问(和我所期望的一样)http:// localhost:3000/companies/2/edit,所以我不能访问嵌入在公司资源中的任何内容 – 2013-04-11 08:35:44

+0

我收到“您无权访问此页面。” – 2013-04-11 08:42:27

回答

0

在你的能力文件中,你只给出了公司管理员对同一公司的[:edit,:update,:destroy]访问权限。

当他试图访问专长控制器中的任何操作时,第一个load_and_authorize_call:company将尝试读取公司。读完公司后,公司会通过公司找到专业人员,并检查用户是否拥有专业特定行为的权利。

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

所以加:读权限或给使用的所有权限:管理公司的管理。

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