【问题标题】:Activeadmin custom pages and CancanActiveadmin 自定义页面和 Cancan
【发布时间】:2014-10-23 15:40:24
【问题描述】:

我正在使用 Activeadmin 和 Cancan 进行用户授权。它在所有标准页面上都能正常工作,但在自定义页面上,我很难将值传递给 Cancan 的能力。 在我的自定义活动管理页面上,我有

 controller do
   prepend_before_filter :filter_method

   def filter_method
     @project = Project.find(params[:id])
     authorize! :show, @project
   end
 end

我拥有的能力

 can :manage, ActiveAdmin::Page, :name => "Project Preview", :poster_id => user.user_id

现在我希望授权! :show, @project 会将项目值传递给 cancan,但我得到的只是“未定义的方法 `poster_id'”。我已经玩了几个小时了,完全被卡住了,所以任何建议都将不胜感激。

【问题讨论】:

    标签: ruby-on-rails-4 activeadmin cancan


    【解决方案1】:

    您需要覆盖页面控制器的authorize_access! 方法;看 ActiveAdmin::PageController 实现参考。

    ActiveAdmin.register_page "Project Page with Authorization" do
      content do
        # page content
      end
    
      controller do
        private
    
        def find_project
          @project = Project.find(params[:id])
        end
    
        def authorize_access!
          find_project
          authorize! :show, @project
        end
      end
    end
    

    然后能力检查将只检查项目而不是 ActiveAdmin::页面:

    can :show, Project, :poster_id => user.user_id
    

    还要注意默认情况下页面视图实际上是index控制器 动作并通过active_admin_config作为授权主题 检查。

    【讨论】:

    • 好消息!谢谢。
    猜你喜欢
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多