ruby-on-rails - rails : prepend_before_action in superclass

标签 ruby-on-rails controller before-filter operator-precedence

我的 ApplicationController 中有一个身份验证方法,我总是想先运行它。我在子 Controller 中还有一个方法,我想在身份验证方法之后运行,但在其他 ApplicationController before_actions 之前运行。换句话说,我想要这个:

ApplicationController
before_action first
before_action third

OtherController < ApplicationController
before_action second

以上导致方法按以下顺序调用:first -> third -> second .
但我想要订单:first -> second -> third .

我试过使用 prepend_before_action,如下所示:
ApplicationController
prepend_before_action first
before_action third

OtherController < ApplicationController
prepend_before_action second

但这导致它去second -> first -> third .

我如何获得订单 first -> second -> third ?

最佳答案

您可以使用 prepend_before_action像这样:

class ApplicationController < ActionController::Base
  before_action :first
  before_action :third
end

class OtherController < ApplicationController
  prepend_before_action :third, :second
end

关于ruby-on-rails - rails : prepend_before_action in superclass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560667/

相关文章:

ruby-on-rails - 如何使用 Rubber gem 运行其他脚本?

linux - 如何在Linux上编写PIC Controller 程序?

ruby-on-rails - 使用 CanCan 来授权资源,但不希望它用于所有操作

ruby-on-rails - 在关联模型中搜索适当的搜索过滤器

ruby-on-rails - 如何在 AWS Elastic Beanstalk 上使用 Resque 运行 Rails 后台作业?

ruby-on-rails - 不确定在哪里(模型或 Controller )定义我的查找方法

php - undefined variable : data (View: C:\cygwin64\home\hp\AddressBook\resources\views\Update\edit.blade.php)

ruby-on-rails - 如何在 Rails 中跳过 json/xml 请求的过滤器之前

ruby-on-rails-3 - 使用 before_filter 检查管理员或用户是否已登录

ruby-on-rails - 为什么没有 “heroku bundle update”?