反向导轨路由:从URL查找动作名称 [英] Reverse rails routing: find the action name from the URL
问题描述
我了解如何转换:controller
,:action,
:etc
转换为网址。我正在寻找相反的方向,如何从URL中找到Rails路由器将调用的操作?
其他人可能有更短的方法来执行此操作,但是如果您只是在评估URL,则转到 ActionController :: Routing :: RouteSet
类
用于 config.routes.rb
map.resources:sessions
要查找的代码是:
ActionController :: Routing :: Routes.recognize_path('/ sessions / new',{:method =>:get})
#=> {:controller => 会话,:action => 'new'}
右:
ActionController :: Routing :: Routes.recognize_path('/ sessions / 1 / edit',{:method =>:get})
#=> {:controller => 会话,:action => ‘edit’,:id => 1}
错误-没有方法
明确添加,它将默认匹配 /:controller /:action /:id
:
ActionController :: Routing :: Routes.recognize_path('/ sessions / 1 / edit')
#=> {:controller => 会话,:action => ‘1’,:id => 'edit'}
如果您愿意采取行动,并且想知道,那就有点通过调用 params [:action]
希望这会有所帮助!
I understand how to turn :controller
, :action,
:etc
into a URL. I'm looking to do the reverse, how can the action that the rails router will call be found from the URL?
someone else might have a shorter way to do this, but if you are just evaluating a URL, then you go to the ActionController::Routing::RouteSet
class
for a config.routes.rb
map.resources :sessions
the code to find is:
ActionController::Routing::Routes.recognize_path('/sessions/new', {:method => :get})
#=> {:controller => 'sessions', :action => 'new'}
Right:
ActionController::Routing::Routes.recognize_path('/sessions/1/edit', {:method => :get})
#=> {:controller => 'sessions', :action => 'edit', :id => 1}
Wrong - without the method
being explicitly added, it will default match to /:controller/:action/:id
:
ActionController::Routing::Routes.recognize_path('/sessions/1/edit')
#=> {:controller => 'sessions', :action => '1', :id => 'edit'}
If you are within the action and would like to know, it is quite a bit easier by calling params[:action]
everything you ever wanted to know about routeset can be found here: http://caboo.se/doc//classes/ActionController/Routing/RouteSet.html#M004878
Hope this helps!
这篇关于反向导轨路由:从URL查找动作名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!