Method: ActionDispatch::Routing::RouteSet#add_route

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

#add_route(mapping, name) ⇒ Object

Raises:

  • (ArgumentError)
[View source]

618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 618

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(<<-MSG.squish)
      Using a dynamic :controller segment in a route is deprecated and
      will be removed in Rails 7.1.
    MSG
  end

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

  route
end