Find HTML verb for action on rails

Is there any way to search for HTML for a given controller action? For example, I would like to be able to associate GET with an index and PUT with an update. I want to be able to do this dynamically based on routes.

I can get action methods for each controller using Controller.action_methods, but this returns a set of action methods. Ideally I would like to form a hash: {:action => :verb}.

+3
source share
3 answers

:methodis part of the hash :conditions, you can go tomap.connect

  map.connect 'post/:id', :controller => 'posts', :action => 'create_comment',
              :conditions => { :method => :get }
0
source

Read the rake route task that will provide insight:

eg:

users GET    /users(.:format)     {:controller=>"users", :action=>"index"}

I guess this is what you are after?

+2

,

def all_routes
    @all_routes ||= Rails.application.routes.routes.map do |route|
          { alias: route.name,
            path: route.path.spec.to_s,
            controller: route.defaults[:controller],
            action: route.defaults[:action],
            verb: route.verb.source.to_s.delete('$'+'^')
          }
    end
end
0

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


All Articles