【问题标题】:Cancan nested_routes restrict acces to :indexCancan 嵌套路由限制对 :index 的访问
【发布时间】:2012-02-21 03:32:03
【问题描述】:

我对 cancan 和嵌套路由有一些问题。

我有这条路线:

resources :companies do
   resources :projects
end

我对公司模型的能力没有问题,但对于项目模型,如果他们不是公司的管理员,我想拒绝对 Project#index 的访问。

下一个代码有效:

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

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

谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 cancan


    【解决方案1】:

    你需要在你的 ProjectsController 中使用这样的东西:

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

    当您尝试访问项目时#index CanCan 将检查能力并根据用户能力拒绝或允许访问

    证明链接https://github.com/ryanb/cancan/issues/209#issuecomment-609043

    希望这是你需要的 =]

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2019-07-10
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    相关资源
    最近更新 更多