I'm trying to collect all the Rails routes, and dump their named_route, path and constraint (if there is one).
I'm able to get the named_route, path, but I'm trying to find a way to get the routes constraint.
routes = Rails.application.routes.routes.map do |route|
path = route.path.spec.to_s.gsub!("(.:format)", '')
# route.constraints always output {} ??
puts route.constraints
name = route.name
[name, path]
end.to_h
Here is an example of a route..
USER_NAME_PATTERN ||= /@[a-z0-9][-a-z0-9]+/
get ":username/:slug", to: 'calcs#show', as: :user_calc , constraints: {username: USER_NAME_PATTERN }
How to programmatically get a Rails Routes constraint?