Ruby on rails 在少数控制器中进行过滤之前

Ruby on rails 在少数控制器中进行过滤之前,ruby-on-rails,ruby-on-rails-4,before-filter,Ruby On Rails,Ruby On Rails 4,Before Filter,我想在以下控制器的一些特定操作中使用名为:realtor\u suscriber的过滤器之前的: realtors/registrations_controller.rb realtors/sessons_controller.rb pro/messages_controller.rb pro/users_controller.rb 我在realtors/registrations\u controller.rb def realtor_suscriber! ....my code here.

我想在以下控制器的一些特定操作中使用名为
:realtor\u suscriber
过滤器之前的

realtors/registrations_controller.rb
realtors/sessons_controller.rb
pro/messages_controller.rb
pro/users_controller.rb
我在
realtors/registrations\u controller.rb

def realtor_suscriber!
....my code here...
end
当然,它不适用于其他控制器中的操作(例如pro/messages\u controller.rb)

我不想在application_controller.rb中定义realtor_suscriber,因为我还有一些其他控制器,它们在筛选前不需要此
,我希望避免在所有其他控制器中使用
跳过筛选前

谢谢你的预告


F.

您可以在应用程序控制器中定义它,跳过特定的控制器

def realtor_suscriber!
  if (params[:controller]=="posts" && %w(new edit show).include?(params[:action])) ||
     (params[:controller]=="comments" && %w(create update destroy).include?(params[:action])) ||
     (params[:controller]=="reviews" && %w(like share tweet).include?(params[:action]))
  return # list out all the controller/action pairs which needs to be skipped.
  #....your code here...
end
使用下列内容

before_filter :realtor_suscriber!, :only => [:abc, :pqr]
#Where abc, pqr are the methods for which you want filter


这非常简单,只需在ApplicationController中定义方法,并将其用作子控制器中的过滤器


子控制器继承自ApplicationController,因此它们始终可以访问此方法。您不需要在ApplicationController中定义这样的过滤器。

您可以定义
realtor\u suscriberApplicationController
中编码>方法并添加

before_filter :realtor_suscriber!

在这些控制器中,您需要它。

谢谢@Satya Kalluri,但是如何指定我要为每个控制器使用的操作?或者我不想使用before筛选器的特定操作。使用params[:controller[和params[:action]用if else实现你想要的。更新了回复。查看。谢谢安东,我太蠢了…我在寻找一个太复杂的解决方案:)谢谢你Billy Chan,正如我对安东说的,我太蠢了…我在寻找一个太复杂的解决方案:)
before_filter :realtor_suscriber!