简体   繁体   English

如何确定已从ApplicationController调用了哪个控制器?

[英]How to determine which controller has been called from ApplicationController?

I would like to put one method inside my ApplicationController and launch it only in case when some controllers are called. 我想在ApplicationController放入一个方法,并仅在调用某些控制器的情况下启动它。 Similarly to devise parameters sanitizer: 与设计参数消毒器类似:

before_action :configure_permitted_parameters, if: :devise_controller?

I've tried: 我试过了:

before_action :recent_discussions, if: :first_controller? || :second_controller? || :third_controller?

However first_controller? 但是first_controller? etc are undefined methods. 等是未定义的方法。

Is there a way to call something in ApplicationController only under specific controllers? 有没有一种方法只能在特定控制器下在ApplicationController调用某些东西?

Presuming you meant || 假设您的意思是|| instead of && in your question's pseudo code ( && makes no sense), this would work: 而不是&&在你的问题的伪代码( &&是没有意义的),这会工作:

FANCY_CONTROLLERS = %w[FirstController SecondController ThirdController]
before_action :recent_discussions,
  if: proc { FANCY_CONTROLLERS.include? "#{controller_name.camelize}Controller" }

Put this in your application controller 把它放在你的应用程序控制器中

class ApplicationController < ActionController::Base
  # more code

  def special_controller?
    controller = params[:controller]
    special_controllers = %w(first second third)
    special_controllers.include? controller
  end
end

And put this in your special controllers 并将其放在您的特殊控制器中

class FirstController < ApplicationController
  before_action :recent_discussions, if: :special_controller?

  # more code
end

You can use request.referrer to get the path of the previous action. 您可以使用request.referrer获取上一个操作的路径。

To get the controller responsible for that action, use: Rails.application.routes.recognize_path(request.referrer)[:controller] 要使控制器负责该操作,请使用: Rails.application.routes.recognize_path(request.referrer)[:controller]

You could define private method def my_controller?; end 您可以定义私有方法def my_controller?; end def my_controller?; end in ApplicationController , and use filter before_action :recent_discussions, if: :my_controller? ApplicationController def my_controller?; end ,并使用过滤器before_action :recent_discussions, if: :my_controller?

Then, in controllers you want this code actually run, redefine def my_controller?; true; end 然后,在要实际运行此代码的控制器中,重新定义def my_controller?; true; end def my_controller?; true; end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在--ApplicationController中确定所请求的Controller - Determine the requested Controller in - ApplicationController 控制器没有从 ApplicationController 继承方法 - Controller not inheriting methods from ApplicationController ArgumentError:ApplicationController 的副本已从模块树中删除但仍处于活动状态 - ArgumentError: A copy of ApplicationController has been removed from the module tree but is still active RSpec / Rails - 如何测试ApplicationController中的方法是否被调用(当我测试子类控制器时)? - RSpec / Rails - How do I test that a method in ApplicationController is called (when I'm testing a subclassed controller)? 如何以编程方式确定Rails中的控制器已将哪些方法声明为“帮助器”方法? - How can I programatically determine which methods have been declared as “helper” methods by a controller in Rails? 确定是否从视图或控制器中调用方法 - Determine if method is called from a view or controller 如何确定提交是否已在Rails中投票 - How to determine if Submission has been voted on in Rails Rspec / Rails测试是否已在控制器内的对象上调用了一种方法 - Rspec/Rails test that a method has been called on an object within a controller 如何确定在控制器操作中呈现哪个视图 - How to determine which view to render in a controller action 如何从控制器确定订单 - How to determine the order from a controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM