Named routes give easy names to your URLs so you can use them in your code without typing the full path. Path helpers are methods that generate these URLs for you.
Named routes and path helpers in Ruby on Rails
get '/articles/:id', to: 'articles#show', as: 'article' # Then in views or controllers: article_path(id) # returns the URL path for the article article_url(id) # returns the full URL including domain
The as: option defines the name of the route.
Use _path for relative URLs and _url for full URLs.
get '/profile', to: 'users#show', as: 'profile' # Usage: profile_path # => "/profile"
:id.get '/articles/:id', to: 'articles#show', as: 'article' # Usage: article_path(5) # => "/articles/5"
resources generates many named routes automatically.resources :books # Usage: books_path # => "/books" new_book_path # => "/books/new" book_path(3) # => "/books/3"
This example shows how to define named routes and use their path helpers to generate URLs. The welcome_path returns "/welcome" and product_path(10) returns "/products/10".
# config/routes.rb Rails.application.routes.draw do get '/welcome', to: 'home#welcome', as: 'welcome' get '/products/:id', to: 'products#show', as: 'product' end # app/controllers/home_controller.rb class HomeController < ApplicationController def welcome render plain: "Welcome page" end end # app/controllers/products_controller.rb class ProductsController < ApplicationController def show render plain: "Product ID: #{params[:id]}" end end # Usage in a view or controller: # welcome_path # => "/welcome" # product_path(10) # => "/products/10"
Always use named routes instead of hardcoding URLs to make your app easier to maintain.
Path helpers automatically handle URL encoding and parameters.
You can see all named routes by running rails routes in your terminal.
Named routes give easy names to URLs for cleaner code.
Path helpers generate URL strings using these names.
Use as: in routes to create named routes and call _path or _url helpers in your app.
Practice
Solution
Step 1: Understand named routes concept
Named routes assign simple names to URL patterns in Rails routes file.Step 2: Identify their purpose
These names help generate URL or path helpers for cleaner, readable code.Final Answer:
To give easy-to-use names for URLs in the app -> Option CQuick Check:
Named routes = URL names [OK]
- Confusing named routes with database features
- Thinking named routes style HTML
- Mixing named routes with authentication
config/routes.rb?Solution
Step 1: Recall correct named route syntax
Rails usesget '/url', to: 'controller#action', as: 'name'format.Step 2: Match options to syntax
get '/profile', to: 'users#show', as: 'profile' matches this exactly; others use invalid keywords.Final Answer:
get '/profile', to: 'users#show', as: 'profile' -> Option AQuick Check:
Correct named route syntax = get '/profile', to: 'users#show', as: 'profile' [OK]
- Using wrong keywords like 'route' or 'named'
- Mixing hash rocket and symbol syntax incorrectly
- Forgetting 'as:' for naming the route
get '/dashboard', to: 'home#dashboard', as: 'dashboard', what does dashboard_path return?Solution
Step 1: Understand named route helper
The named route 'dashboard' createsdashboard_pathhelper returning the URL path string.Step 2: Match helper output
Since route is '/dashboard',dashboard_pathreturns '/dashboard'.Final Answer:
'/dashboard' -> Option DQuick Check:
dashboard_path = '/dashboard' [OK]
- Confusing _path with _url helpers
- Expecting controller/action strings
- Adding extra suffixes like '_path' in output
get '/items', to: 'items#index', as: items_pathSolution
Step 1: Check named route naming
Theas:option expects a symbol or string name, not a method call likeitems_path.Step 2: Identify correct usage
It should beas: :itemsoras: 'items'to createitems_pathhelper.Final Answer:
The named route should be a symbol or string, not a method call -> Option BQuick Check:
Use symbol/string for 'as:' [OK]
- Passing helper method name instead of symbol/string
- Misunderstanding 'as:' purpose
- Thinking HTTP verb or path is wrong here
/users/:id/edit. Which route definition and helper usage is correct?Solution
Step 1: Confirm correct URL pattern
The edit user URL is conventionally '/users/:id/edit'.Step 2: Check named route and helper usage
Route:get '/users/:id/edit', to: 'users#edit', as: 'edit_user'; Helper:edit_user_path(5)uses string for 'as:' and callsedit_user_path(5)which is correct. Route:get '/users/:id/edit', to: 'users#edit', as: :edit_user; Helper:edit_user_urluses symbol but callsedit_user_urlwithout argument, which is incomplete.Final Answer:
Route: get '/users/:id/edit', to: 'users#edit', as: 'edit_user'; Helper: edit_user_path(5) -> Option AQuick Check:
Correct path + named route + helper call = Route:get '/users/:id/edit', to: 'users#edit', as: 'edit_user'; Helper:edit_user_path(5)[OK]
- Swapping :id and 'edit' in URL path
- Using _url helper without required id argument
- Using symbol/string inconsistently for 'as:'
Ready to prove it?
Pass the test and this pattern moves unknown
