具有before_filters的应用程序控制器,它执行第一个和最后一个

时间:2014-04-25 22:26:56

标签: ruby-on-rails

我想设置我的应用程序控制器在所有其他before_filters之前执行before_filter,并在所有其他before_filters之后设置before_filter。我怎么能这样做?

在rails中,首先执行应用程序控制器,然后执行其他控制器。所以我可以先让授权发生,或者让more_info发生在最后,但不是两个......我尝试了各种prepend_before_filters,append_before_filters没有运气(主要问题似乎是append_before_filter是before_filter的别名,所以它实际上并没有最后追加more_info)

以下示例:

class ApplicationController < ActionController::Base
  before_filter :authorize # should be first
  before_filter :some_other_action
  before_filter :another_action 
  append_before_filter :more_info # should be last
end

class OtherController < ApplicationController #Approximately 30 other controllers
  before_filter :get_other #all actions in all controllers should be in between authorize and more_info
  before_filter :various_action
  before_filter :different_action 
  before_filter :mas_acciones 
  before_filter :mais_acoes 
end
预期


1.授权
2. get_other
3. more_info

实际:
1.授权
2. more_info
3. get_other

使用Rails 4.0.3

1 个答案:

答案 0 :(得分:0)

请尝试这样:

class ApplicationController < ActionController::Base
 before_filter :authorize # should be first
 before_filter :more_info # should be last
end

class OtherController < ApplicationController
  append_before_filter :authorize,:get_other,:more_info #should be in between authorize and more_info
end 

get_other将位于authorize和more_info之间。