2012-02-21 152 views
2

我有一些cancan和嵌套路由的问题。Cancan nested_routes限制访问权限:索引

我有这样的路线:

resources :companies do 
    resources :projects 
end 

我有能力为公司的模式,但对于项目模式,我希望拒绝访问Project#指标,如果他们不是公司的管理没有问题。

下一个代码工作:

can :show, Company do |company| 
    if user.admins.include?(company) #check if the user is admin of the company 
     can :index, Schedule, :company_id => company.id 
    end 
end 

可是我该怎么办:

can? :index, Project 

我试图通过改名的方式类似:

can :index_projects, Company do |company| 
    if user.admins.include?(company) #check if the user is admin of the company 
     can :index, Schedule, :company_id => company.id 
    end 
end 

及用途:

can? :index_projects, @company 

但它不起作用。你知道该怎么做吗?

谢谢。

回答

3

你需要使用这样的事情在你的ProjectsController:

class ProjectsController < ApplicationController 
    def index 
    authorize! :index, Ability 
    @projects = Project.order(:created_at) 
    end 
end 

,当您`ll尝试访问项目#指数惨惨将检查能力,并拒绝或允许访问根据用户的能力

prooflink https://github.com/ryanb/cancan/issues/209#issuecomment-609043

希望这是你需要什么=]

+0

我 你能解释我STE,这是不行的p为此? – ashvin 2016-07-12 07:24:04

+0

@ashvin按照我上面发布的链接(https://github.com/ryanb/cancan/issues/209#issuecomment-609043) – jbmeerkat 2016-07-20 16:01:07