Scotty Weeks
Scotty Weeks

Reputation: 53

How do I access the route information (HTTP verb, action name, etc . . .) from within a controller in Rails?

Say I'm within a controller and I want to get a list of the actions and HTTP verbs that it supports, how would I do that?. E.g.

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

Or something equivalent.

Upvotes: 4

Views: 589

Answers (1)

daniel
daniel

Reputation: 9845

Something like this:

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.

Upvotes: 3

Related Questions