如何找到Rails中的当前路由?

luc*_*uca 204 ruby routing ruby-on-rails

我需要知道Rails中过滤器中的当前路由.我该怎么知道它是什么?

我正在做REST资源,并且看不到命名路由.

IAm*_*NaN 289

如果您在视图中尝试特殊情况,可以使用current_page?如下:

<% if current_page?(:controller => 'users', :action => 'index') %>
Run Code Online (Sandbox Code Playgroud)

......或者行动和身份......

<% if current_page?(:controller => 'users', :action => 'show', :id => 1) %>
Run Code Online (Sandbox Code Playgroud)

......或命名路线......

<% if current_page?(users_path) %>
Run Code Online (Sandbox Code Playgroud)

...和

<% if current_page?(user_path(1)) %>
Run Code Online (Sandbox Code Playgroud)

因为current_page?需要控制器和动作,当我只关心控制器时,我current_controller?在ApplicationController中创建一个方法:

  def current_controller?(names)
    names.include?(current_controller)
  end
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

<% if current_controller?('users') %>
Run Code Online (Sandbox Code Playgroud)

...也适用于多个控制器名称......

<% if current_controller?(['users', 'comments']) %>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,您还可以使用current_page?命名路由:current_page?(users_path) (27认同)
  • `controller_name`和`action_name`也适用于帮助器和视图中的这类东西. (4认同)

Swa*_*and 193

要找出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"
Run Code Online (Sandbox Code Playgroud)

找出路线,即控制器,动作和参数:

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
Run Code Online (Sandbox Code Playgroud)

  • 当前控制器和动作总是在`params [:controller]`和`params [:action]`中可用.但是,除此之外,如果您想识别路线,此API将不再可用.它现在已转移到`ActionDispatch :: Routing`,我还没有尝试过`recogn_path`. (36认同)
  • 最好使用`request.path`来查找当前路径. (35认同)
  • 您是否碰巧知道这是否是在Rails 3中执行此操作的相同/正确方法?我确信它仍然可以使用,但我只是想确保我遵守最新的惯例. (2认同)
  • 您也可以调用`request.env ['ORIGINAL_FULLPATH']`来在路径中包含可能的参数,请参阅下面的答案. (2认同)
  • 如果在路由中设置了trailing_slash,则current_uri = request.env ['PATH_INFO']不起作用 (2认同)

dch*_*cke 143

我可以在2015年提出最简单的解决方案(使用Rails 4验证,但也应该使用Rails 3)

request.url
# => "http://localhost:3000/lists/7/items"
request.path
# => "/lists/7/items"
Run Code Online (Sandbox Code Playgroud)

  • 这个答案肯定需要更多的选票 (7认同)
  • 如果您想要视图中的 id: &lt;%= request.path_parameters[:id] %&gt; (2认同)

Luc*_*nan 19

你可以这样做

Rails.application.routes.recognize_path "/your/path"
Run Code Online (Sandbox Code Playgroud)

它适用于rails 3.1.0.rc4


Kin*_*ain 11

在rails 3中,您可以通过Rails.application.routes对象访问Rack :: Mount :: RouteSet对象,然后直接调用识别它

route, match, params = Rails.application.routes.set.recognize(controller.request)
Run Code Online (Sandbox Code Playgroud)

获得第一个(最佳)匹配,以下块形式循环匹配路由:

Rails.application.routes.set.recognize(controller.request) do |r, m, p|
  ... do something here ...
end
Run Code Online (Sandbox Code Playgroud)

获得路线后,您可以通过route.name获取路线名称.如果你需要获取特定URL的路由名称,而不是当前的请求路径,那么你需要模拟一个假的请求对象传递到机架,检查ActionController :: Routing :: Routes.recognize_path以查看他们是怎么做的.

  • 错误:`未定义方法'识别'#Journey :: Routes:0x007f893dcfa648> (4认同)

neo*_*ate 7

基于@AmNaN建议(更多细节):

class ApplicationController < ActionController::Base

 def current_controller?(names)
  names.include?(params[:controller]) unless params[:controller].blank? || false
 end

 helper_method :current_controller?

end
Run Code Online (Sandbox Code Playgroud)

现在,您可以在导航布局中将其标记为将列表项标记为活动:

<ul class="nav nav-tabs">
  <li role="presentation" class="<%= current_controller?('items') ? 'active' : '' %>">
    <%= link_to user_items_path(current_user) do %>
      <i class="fa fa-cloud-upload"></i>
    <% end %>
  </li>
  <li role="presentation" class="<%= current_controller?('users') ? 'active' : '' %>">
    <%= link_to users_path do %>
      <i class="fa fa-newspaper-o"></i>
    <% end %>
  </li>
  <li role="presentation" class="<%= current_controller?('alerts') ? 'active' : '' %>">
    <%= link_to alerts_path do %>
      <i class="fa fa-bell-o"></i>
    <% end %>
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

对于usersalerts路线,current_page?就足够了:

 current_page?(users_path)
 current_page?(alerts_path)
Run Code Online (Sandbox Code Playgroud)

但是对于嵌套路由和对控制器的所有操作的请求(与之相当items),current_controller?对我来说是更好的方法:

 resources :users do 
  resources :items
 end
Run Code Online (Sandbox Code Playgroud)

第一个菜单条目对以下路径有效:

   /users/x/items        #index
   /users/x/items/x      #show
   /users/x/items/new    #new
   /users/x/items/x/edit #edit
Run Code Online (Sandbox Code Playgroud)


Dar*_*rme 6

您是否还需要参数

current_fullpath = request.env['ORIGINAL_FULLPATH']
# 如果你正在浏览 http://example.com/my/test/path?param_n=N 
# 那么 current_fullpath 将指向 "/my/test/path?param_n=N"

请记住,您始终可以<%= debug request.env %>在视图中调用以查看所有可用选项。


dip*_*ent 5

或者,更优雅地: request.path_info

资料来源:
索取架文件