【问题标题】:Not only define action for before_filter but also controller不仅为 before_filter 定义动作,还为控制器定义动作
【发布时间】:2013-10-16 19:58:18
【问题描述】:

是否有可能我不仅为 before_filter 定义了一个动作,而且还为动作的控制器定义了一个动作?我想将 before_filter 方法放入我的 application_controller 但是当我在那里定义例如:

before_filter :authorize, except: [:index]

所有具有索引操作的控制器都会受到影响! 我想写这样的东西:

before_filter :authorize, except: [user#index]

谢谢

【问题讨论】:

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


    【解决方案1】:

    你不能按照你的要求去做。你能做的是

    class ApplicationController < ActionController::Base
      before_filter :authorize
      ...
    end
    
    
    class UsersController < ApplicationController
      skip_before_filter :authorize, :only => [:index]
      ...
    end
    

    【讨论】:

      【解决方案2】:

      为什么不把 before 过滤器放在你的 users_controller 中?

      class UsersController < ApplicationController
        before_filter :authorize, except: [:index]
        ...
      end
      

      【讨论】:

      • 但我打算将其放入应用程序控制器以节省时间和工作
      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多