79158080

Date: 2024-11-05 07:51:23
Score: 0.5
Natty:
Report link

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.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Is this
  • Low reputation (0.5):
Posted by: Rainie