我需要在Rails中的过滤器中知道当前路由。我怎么才能知道是什么?

我正在使用REST资源,没有看到命名路由。


当前回答

你可以请求。env['REQUEST_URI']查看完整的请求URI..它将输出如下内容

http://localhost:3000/client/1/users/1?name=test

其他回答

我假设你指的是URI:

class BankController < ActionController::Base
  before_filter :pre_process 

  def index
    # do something
  end

  private
    def pre_process
      logger.debug("The URL" + request.url)
    end
end

根据你下面的评论,如果你需要控制器的名称,你可以简单地这样做:

  private
    def pre_process
      self.controller_name        #  Will return "order"
      self.controller_class_name  # Will return "OrderController"
    end

或者更优雅地说:request.path_info

来源: 请求机架文档

你可以通过rake:routes看到所有的路由(这可能对你有帮助)。

要查找URI:

current_uri = request.env['PATH_INFO']
# If you are browsing http://example.com/my/test/path, 
# then above line will yield current_uri as "/my/test/path"

找到路径,即控制器,动作和参数:

path = ActionController::Routing::Routes.recognize_path "/your/path/here/"

# ...or newer Rails versions:
#
path = Rails.application.routes.recognize_path('/your/path/here')

controller = path[:controller]
action = path[:action]
# You will most certainly know that params are available in 'params' hash

request.url

请求。获取除基本url之外的路径