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)
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)
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)
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以查看他们是怎么做的.
基于@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)
对于users
和alerts
路线,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)
您是否还需要参数:
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 %>
在视图中调用以查看所有可用选项。