Redirects and Wildcard Routes
suggest changeTo 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents