How can I guess the action name from url in Rails?

For example, I have a url generated by something_pathor methods something_url. I need to know the action name from this URL.

+3
source share
4 answers

The Rails routing system works in two ways: it recognizes and creates URLs. You need a recognition method, as shown in the following example:

ActionController::Routing::Routes.recognize_path('/mycontroller/myaction', :method => :get)

Assuming the URL was generated with something_pathor something_url, it returns:

{ :action => 'myaction', :controller => 'mycontroller' }

From which you can extract part of the action.

+2
source

. rake routes / , . HTTP-, .

+3

If you are in view mode, you can use

  • action_nameto access the processing action
  • controller_nameto access the processing controller
0
source

In Rails 3:

Rails.application.routes.recognize_path("http://example.ltd/registrants")
=> => {:action=>"index", :controller=>"registrants"}

If you are in the engine, events

Events::Engine.routes.recognize_path("/registrants")
=> {:action=>"index", :controller=>"events/registrants"}
0
source

Source: https://habr.com/ru/post/1727460/


All Articles