回答
要找出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"
要找出途径,即控制器,动作,而params:
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
我假设你的意思是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_parameters('控制器')似乎不适合我。 – luca 2009-07-30 08:12:40
`self.controller_name`和`self.controller_class_name`不需要'self.` – weltschmerz 2015-07-24 18:06:07
如果你想特例的东西在一个视图中,可以使用current_page?
如:
<% if current_page?(:controller => 'users', :action => 'index') %>
...或动作和id ...
<% if current_page?(:controller => 'users', :action => 'show', :id => 1) %>
...或命名路线...
<% if current_page?(users_path) %>
...和
<% if current_page?(user_path(1)) %>
因为current_page?
同时需要一个控制器和动作,当我关心的只是控制器我在ApplicationController中制作current_controller?
方法:
def current_controller?(names)
names.include?(current_controller)
end
而且使用这样的:
<% if current_controller?('users') %>
...这还与多个控制器名称...
<% if current_controller?(['users', 'comments']) %>
在Rails 3,您可以访问机架::摩:: RouteSet对象经由Rails.application.routes对象,然后在其上调用识别直接
route, match, params = Rails.application.routes.set.recognize(controller.request)
其获取第一个(最好)匹配,以下块形式循环通过匹配的路由:
Rails.application.routes.set.recognize(controller.request) do |r, m, p|
... do something here ...
end
一旦有了路由,就可以通过route.name获取路由名称。如果您需要获取特定URL的路由名称,而不是当前请求路径,那么您需要模拟虚拟请求对象以传递到机架,查看ActionController :: Routing :: Routes.recognize_path以查看他们是如何做的。
你可以做到这一点
Rails.application.routes.recognize_path "/your/path"
它为我在轨3.1.0.rc4
如果您还需要参数:
current_fullpath = request.env['ORIGINAL_FULLPATH'] # If you are browsing http://example.com/my/test/path?param_n=N # then current_fullpath will point to "/my/test/path?param_n=N"
请记住,你可以始终致电<%= debug request.env %>
以查看所有可用选项。
基于@AmNaN建议(详细信息):
class ApplicationController < ActionController::Base
def current_controller?(names)
names.include?(params[:controller]) unless params[:controller].blank? || false
end
helper_method :current_controller?
end
现在你可以如叫它在导航布局标记列表项为活动:
<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>
对于users
和alerts
路线,current_page?
就足够了:
current_page?(users_path)
current_page?(alerts_path)
但随着嵌套路线和要求提供控制器的所有动作(与items
可比性),current_controller?
对我来说是更好的方法:
resources :users do
resources :items
end
的第一个菜单项是这样活跃了以下路线:
/users/x/items #index
/users/x/items/x #show
/users/x/items/new #new
/users/x/items/x/edit #edit
或者,更优雅:request.path_info
最简单的解决方案,我可以在2015年拿出(验证使用轨道4,但也应该使用Rails工作3)
request.url
# => "http://localhost:3000/lists/7/items"
request.path
# => "/lists/7/items"
request.url
Request的q若要获取路径,除了基本URL
你可以做request.env['REQUEST_URI']
看到完整的请求的URI ..这将输出类似下面
http://localhost:3000/client/1/users/1?name=test
- 1. 如何在laravel 5.1中查找当前路线?
- 2. 如何找出当前在Rails 2.3.5中登录的用户?
- 3. 在python中如何找出os.walk()的当前路径?
- 4. Rails助手找到当前路径
- 5. 如何获得canActive路线中的当前路线?
- 6. 如何找出当前uiviewcontroller
- 7. 在Backbone/Marionette中追加当前路线?
- 8. 在Rails中,如何获取当前url(但没有路径)
- 9. Rails url_for找不到出现在Rake Routes中的路线?
- 10. 如何在boost中找到当前正在运行的线程?
- 11. 如何在自定义路线中获取当前用户?
- 12. 如何在HtmlHelper中获取当前路线?
- 13. 如何在Rails中找到当前的双周期
- 14. 当前路线模式
- 15. 如何在Visual Studio Debugger中找到当前StackFrame的列线?
- 16. Ruby on Rails:你如何在命名路线前缀?
- 17. 力铁路由器去当前路线
- 18. 如何在XSLT中输出当前元素路径?
- 19. 在Rails中翻译路线
- 20. 如何在express.js中退出路线?
- 21. 如何获得Symfony 2中的当前路线?
- 22. 如何找出当前正在执行的脚本的路径?的
- 23. 如何在j2me中找出当前日期的时间戳?
- 24. 在Rails的路线
- 25. 如何获得执行当前线程的文件的当前路径?
- 26. 塔架 - 如何获取当前控制器和动作(当前路线)?
- 27. 找出当前位置
- 28. 找出当前CPU频率
- 29. 如何从谷歌导航中检索当前的路线信息在Android中
- 30. 如何找出如果当前帧具有在Emacs
什么是你试图用这个来完成?当你说“路线”时,你的意思是“URI”吗? – jdl 2009-07-30 01:14:53
有关如何在[中间件](http://stackoverflow.com/q/42691729/1610034)中获取它的任何想法。 – Saurabh 2017-03-14 08:03:46