【问题标题】:Cancan and custom route for member resource会员资源的 Cancan 和自定义路由
【发布时间】:2014-12-09 06:52:09
【问题描述】:

我有一条看起来像这样的路线:

routes.rb

resources :blog do
  member do
    get :dashboard, to: 'admin/postings#index'
  end

  resources :postings, only: [:index]
end

我正在使用 cancan gem 来限制对博客页面的访问,有点像这样:

能力.rb

def initialize(user)
  if user.has_role?(:blog_admin, :any)
    can :access, Blog, id: user.blog.id
    can :access, Posting, blog_id: user.blog.id
  end
end

我遇到的问题是路径blog/:id/postings 工作得很好,我可以访问它,但是blog/:id/dashboard 给了我一个拒绝访问错误。如果我通过同一个控制器进行路由,为什么 url 是什么重要?

【问题讨论】:

    标签: ruby-on-rails ruby routes cancan


    【解决方案1】:

    所以我能够弄清楚。 rails 为仪表板生成的路由看起来像这样,blog/:id/dashboard,但为发布生成的路由看起来像 blog/:blog_id/postings。所以在 cancan 中,can 方法在末尾接受一个条件哈希,它将检查params[:blog_id] 是否与user.blog.id 匹配。这就是问题所在。我要去的路线只定义了params[:id],而不是params[:blog_id]。所以我只是通过从博客资源中删除它并定义它来更改仪表板路由:get '/blogs/:blog_id/dashboard', to: 'admin/postings#index'

    【讨论】:

      猜你喜欢
      • 2011-01-17
      • 2014-08-22
      • 2015-11-04
      • 2011-07-05
      • 1970-01-01
      • 2011-10-09
      • 2019-07-02
      • 2015-09-26
      • 2016-09-28
      相关资源
      最近更新 更多