Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 应用程序控制器,带有最先和最后执行的before_过滤器_Ruby On Rails - Fatal编程技术网

Ruby on rails 应用程序控制器,带有最先和最后执行的before_过滤器

Ruby on rails 应用程序控制器,带有最先和最后执行的before_过滤器,ruby-on-rails,Ruby On Rails,我想将我的应用程序控制器设置为在所有其他before_过滤器之前执行before_过滤器,以及在所有其他before_过滤器之后执行before_过滤器。我该怎么做 在rails中,首先执行应用程序控制器,然后执行其他控制器。因此,我可以先获得授权,或最后获得更多信息,但不能同时获得这两种信息。。。。我尝试过各种prepend_before_过滤器,append_before_过滤器,但运气不好(主要问题似乎是append_before_过滤器是before_过滤器的别名,因此它实际上不会最后附

我想将我的应用程序控制器设置为在所有其他before_过滤器之前执行before_过滤器,以及在所有其他before_过滤器之后执行before_过滤器。我该怎么做

在rails中,首先执行应用程序控制器,然后执行其他控制器。因此,我可以先获得授权,或最后获得更多信息,但不能同时获得这两种信息。。。。我尝试过各种prepend_before_过滤器,append_before_过滤器,但运气不好(主要问题似乎是append_before_过滤器是before_过滤器的别名,因此它实际上不会最后附加更多的_信息)

示例如下:

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
class ApplicationController
预期:
1.授权
2.获取其他
3.更多信息

实际值:
1.授权
2.更多信息
3.找其他人

使用Rails 4.0.3

请像这样尝试:

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 
class ApplicationController

get\u other
将介于authorize和more\u info之间。

这确实有效,但我希望找到一种解决方案,其他控制器不需要知道应用程序控制器的方法。还有其他可能性吗?我没有正确理解你的问题?实际上你需要什么?我在原来的问题上补充了更多的信息。我有许多其他控制器,每个控制器都有许多前置过滤器。我想减少重复代码的数量。根据您的回答,我将在整个应用程序中重复“在前追加”筛选行数百次。不确定这是否正确,请创建一个数组,并将控制器类名像
array=[“OtherController”]
一样放入
array.each do | kclass | kclass.constantize.class|eval do append|u在筛选之前:authorize,:get|other,:更多信息结束
像你尝试的那样,我明天会尝试更好的方法来完成。