Rails - Extract HTTP Verb From Named Route

Viewed 2705

Is there a way in Rails to extract the HTTP verb(s) associated with a route? For example, given a route like this:

match 'users', to: 'users#show', via: [:get, :post]

Can I achieve something like this?

users_path.respond_to?(:get) (obviously #respond_to is not the right method)

The closest I've managed to come is by doing the following, but it doesn't really seem satisfactory.

Rails.application.routes.routes.named_routes["users"].constraints[:request_method] # => /^GET$/

For context, I have an action that sets a cookie and then does a redirect_to :back, but this action is available globally across the entire site (it's in the footer). So, if a user happens to be in a flow, and one of those routes only accepts POSTs, the redirect fails because the request issued is a GET.

2 Answers
Related