[英]How can I guess action name from url in Rails?
For example I have a url generated by something_path
or something_url
methods. 例如,我有一个由
something_path
或something_url
方法生成的url 。 I need to know action name from that url . 我需要知道该网址的操作名称。
You don't need to guess. 你不需要猜测。 You can run
rake routes
from the Terminal/Command Prompt to get a list of all the routes in your application. 您可以从终端/命令提示符运行
rake routes
,以获取应用程序中所有路由的列表。 The output includes the HTTP method used, as well as the controller and action invoked. 输出包括使用的HTTP方法,以及调用的控制器和操作。
The routing system of Rails works two ways, it recognizes and builds URLs. Rails的路由系统有两种工作方式,它可以识别和构建URL。 You need the recognize_path method, like the following example shows:
您需要recogn_path方法,如下例所示:
ActionController::Routing::Routes.recognize_path('/mycontroller/myaction', :method => :get)
Assuming that the URL was generated with something_path
or something_url
, it returns: 假设URL是使用
something_path
或something_url
生成的,则返回:
{ :action => 'myaction', :controller => 'mycontroller' }
From which you are able to extract the action part. 您可以从中提取动作部分。
If you are in a view, you can use 如果您在视图中,则可以使用
action_name
to get access to the processing action action_name
可以访问处理操作 controller_name
to get access to the processing controller controller_name
可以访问处理控制器 In Rails 3: 在Rails 3中:
Rails.application.routes.recognize_path("http://example.ltd/registrants")
=> => {:action=>"index", :controller=>"registrants"}
If you're in an engine, Events 如果你在引擎,事件
Events::Engine.routes.recognize_path("/registrants")
=> {:action=>"index", :controller=>"events/registrants"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.