Is this what you want?
def extract_routes_for_model(model_name)
Rails.application.routes.routes.map do |route|
verb = route.verb.match(/[A-Z]+/).to_s
path = route.path.spec.to_s
controller_action = route.defaults[:controller]
action = route.defaults[:action]
helper = Rails.application.routes.url_helpers.method_defined?("#{route.name}_path") ? "#{route.name}_path" : nil
if controller_action&.include?(model_name.underscore.pluralize)
{
method: verb,
path: path,
helper: helper,
action: action
}
end
end.compact
end
Use: extract_routes_for_model("Post")
The output will be an array of hashes containing information for each corresponding path.