How to get the current URL route pattern in rails?

Suppose I have a route defined as follows:

constraints MyRouteConstraint.new do get ':param/:param1/:param2', to: 'controller#action' end 

How can I get the pattern ': param /: param1 /: param2' in my controller? I know that Rails.application.routes.recognize_path is "/ param / param1 / param2", but it causes the error "No route matching"

update:

 Rails.application.routes.router.recognize(request) do |route, matches, parameters| return route.path.spec.to_s if route.path.required_names.all? { |p| request.params.keys.include? p } end.flatten.compact.first 
+7
ruby-on-rails ruby-on-rails-4 routes
source share
1 answer

Not too sure if this is what you are looking for, it is intended to be used in your presentations.

You can try request.fullpath

Returns the full path of the String, including parameters for the last requested URL.

 # get "/articles" request.fullpath # => "/articles" # get "/articles?page=2" request.fullpath # => "/articles?page=2" 

See the docs here:

http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url

0
source share

All Articles