在应用程序控制器的 before 过滤器中。
class ApplicationController < ActionController::Base
before_filter :authenticate
def authenticate
# How do we know which controller and action was targetted?
end
end
在应用程序控制器的 before 过滤器中。
class ApplicationController < ActionController::Base
before_filter :authenticate
def authenticate
# How do we know which controller and action was targetted?
end
end
class ApplicationController < ActionController::Base
before_filter :authenticate
def authenticate
# How do we know which controller and action was targetted?
params[:controller]
params[:action]
# OR
controller.controller_name
controller.action_name
end
end
params[:action]
。但是由于某种原因,在过去的一个月左右,它停止工作了。现在它变成了一个空哈希。然而,controller.action_name
仍然可以工作。我们使用的是Rails 3.2。不确定为什么它停止工作,但我建议使用controller.action_name
,因为它似乎更可靠。 - Joshua Pinterbefore_filter :check_if_locked
def check_if_locked
puts action_name
puts controller_name
end
您可以使用以下方法获取完整的URL对象:
url = Rails.application.routes.recognize_path(request.env['PATH_INFO'])
现在,您可以获取以下组件:
url[:controller]
url[:action]
在请求/响应生命周期中,默认情况下还可以分别使用params[:controller]
和params[:action]
。
url = Rails.application.routes.recognize_path(request.env['PATH_INFO'], { :method => request.env['REQUEST_METHOD'] })
。 - Chuck Batsonrequest.parameters['controller']
request.parameters['action']