Method: ActionDispatch::Routing::RouteSet#add_route

Defined in:
lib/action_dispatch/routing/route_set.rb

#add_route(mapping, name) ⇒ Object

Raises:

  • (ArgumentError)


643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/action_dispatch/routing/route_set.rb', line 643

def add_route(mapping, name)
  raise ArgumentError, "Invalid route name: '#{name}'" unless name.blank? || name.to_s.match(/^[_a-z]\w*$/i)

  if name && named_routes[name]
    raise ArgumentError, "Invalid route name, already in use: '#{name}' \n" \
      "You may have defined two routes with the same name using the `:as` option, or " \
      "you may be overriding a route already defined by a resource with the same naming. " \
      "For the latter, you can restrict the routes created with `resources` as explained here: \n" \
      "https://guides.rubyonrails.org/routing.html#restricting-the-routes-created"
  end

  route = @set.add_route(name, mapping)
  named_routes[name] = route if name

  if route.segment_keys.include?(:controller)
    ActionDispatch.deprecator.warn("      Using a dynamic :controller segment in a route is deprecated and\n      will be removed in Rails 9.0.\n    MSG\n  end\n\n  if route.segment_keys.include?(:action)\n    ActionDispatch.deprecator.warn(<<-MSG.squish)\n      Using a dynamic :action segment in a route is deprecated and\n      will be removed in Rails 9.0.\n    MSG\n  end\n\n  route\nend\n".squish)