Redirects and Wildcard Routes

suggest change

To offer a short URL that lands on a route you already have, redirect:

# config/routes.rb
TestApp::Application.routes.draw do
  get "courses/:course_name" => redirect("/courses/%{course_name}/lessons"), as: "course"
end

redirect sends one route to another. To keep a dynamic segment, put it in %{course_name} inside a single-quoted string. A double-quoted string would interpolate in Ruby before redirect sees the placeholder.

as: "course" names the helper course_path. Check the result with rails routes. TestApp::Application.routes.draw is the older style. Current apps use Rails.application.routes.draw.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents