How to access route information (HTTP verb, action name, etc.) from a controller in Rails?

Let's say I'm in the controller and I want to get a list of actions and HTTP verbs that it supports, how would I do that ?. For instance.

controller.actions # { [ :show, :get ], [ :update, :put ], . . . } 

Or something similar.

+4
source share
2 answers

Something like that:

 controller = "users" controller_routes = [] Rails.application.routes.routes.each do |route| if route.to_s.include?(":controller=>\"#{controller}\"") controller_routes << route end end puts controller_routes 

You get the idea.

+4
source

To list the http methods (GET, POST, PUT, DELETE) that are valid for each controller action run:

 rake routes 

It will list which http methods are configured for all controllers.

You change which http methods are used when editing /config/routes.rb

RailsGuide has a great explanation here .

-2
source

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


All Articles