📖 Scenario: You are building a simple Rails app that shows details about books. Each book has a unique ID. You want to create a route that uses this ID in the URL to show the right book.
🎯 Goal: Build a Rails route that uses a route parameter called :id to capture the book's ID from the URL and pass it to the controller.
📋 What You'll Learn
Create a route with a parameter named :id in config/routes.rb
Define a controller action show in BooksController
Use the route parameter params[:id] inside the show action
Add a view file to display the book ID
💡 Why This Matters
🌍 Real World
Web apps often show details for items like products, users, or posts by using route parameters in the URL.
💼 Career
Understanding route parameters is essential for building dynamic web pages and APIs in Rails development jobs.
Progress0 / 4 steps
1
Create the books data hash
Create a hash called BOOKS in app/controllers/books_controller.rb with these exact entries: 1 => 'The Hobbit', 2 => '1984', 3 => 'Pride and Prejudice'.
Ruby on Rails
Hint
Think of BOOKS as a small dictionary that stores book names by their IDs.
2
Add the route with parameter :id
In config/routes.rb, add a route that maps GET /books/:id to the show action of BooksController.
Ruby on Rails
Hint
Use get '/books/:id', to: 'books#show' to capture the :id from the URL.
3
Define the show action using params[:id]
In BooksController, define a show method that sets @book_name to the value from BOOKS using params[:id].to_i.
Ruby on Rails
Hint
Use params[:id].to_i to convert the route parameter to a number and find the book.
4
Create the show view to display the book name
Create a file app/views/books/show.html.erb with this exact content: <h1>Book: <%= @book_name %></h1>.
Ruby on Rails
Hint
This view shows the book name inside an <h1> heading.
Practice
(1/5)
1. What does a route parameter in Rails do? get '/users/:id', to: 'users#show'
easy
A. Captures part of the URL as a variable to use in the controller
B. Defines a fixed URL path without variables
C. Specifies the HTTP method to use
D. Sets the controller action to redirect to another page
Solution
Step 1: Understand the colon in the route path
The colon before id means this part of the URL is a variable, called a route parameter.
Step 2: Role of route parameters in Rails
This variable is captured from the URL and passed to the controller action for use, like finding a user by ID.
Final Answer:
Captures part of the URL as a variable to use in the controller -> Option A
Quick Check:
Route parameter = URL variable [OK]
Hint: Colon before name means URL part is a variable [OK]
Common Mistakes:
Thinking route parameters define fixed paths
Confusing route parameters with HTTP methods
Assuming route parameters redirect to other pages
2. Which of the following is the correct syntax to define a route with a parameter named post_id in Rails?
easy
A. get '/posts/post_id', to: 'posts#show'
B. get '/posts/{post_id}', to: 'posts#show'
C. get '/posts/<post_id>', to: 'posts#show'
D. get '/posts/:post_id', to: 'posts#show'
Solution
Step 1: Identify correct route parameter syntax
In Rails, route parameters use a colon before the name, like :post_id.
Step 2: Compare options
Only get '/posts/:post_id', to: 'posts#show' uses the colon syntax correctly. Others use incorrect braces or no colon.
Final Answer:
get '/posts/:post_id', to: 'posts#show' -> Option D
Quick Check:
Colon before parameter name = correct syntax [OK]
Hint: Use colon before parameter name in route path [OK]
Common Mistakes:
Using curly braces or angle brackets instead of colon
Omitting colon before parameter name
Confusing parameter syntax with query strings
3. Given this route: get '/products/:id', to: 'products#show' And this controller code:
def show
@product_id = params[:id]
end
What will @product_id be if the URL is /products/42?
medium
A. The string '42'
B. The integer 42
C. nil
D. An error because params[:id] is undefined
Solution
Step 1: Understand route parameter capture
The :id in the route captures the URL part '42' as a string in params[:id].
Step 2: Assigning params value in controller
The controller assigns @product_id = params[:id], so @product_id is the string '42'. Rails does not convert it automatically to integer.
Final Answer:
The string '42' -> Option A
Quick Check:
Route params are strings by default [OK]
Hint: Route params are strings unless converted [OK]
Common Mistakes:
Assuming params[:id] is an integer automatically
Expecting nil if parameter is present
Thinking params[:id] causes an error if defined in route
4. Identify the error in this route definition:
get '/orders/:order_id/items/:item_id', to: 'orders#item'
Controller action:
def item
@order = Order.find(params[:id])
@item = Item.find(params[:item_id])
end
medium
A. Route path should not have two parameters
B. params[:id] is incorrect; should be params[:order_id]
C. Controller action name should be 'show' not 'item'
D. Item.find should use params[:id] instead of params[:item_id]
Solution
Step 1: Check route parameters names
The route defines parameters :order_id and :item_id, so params keys are :order_id and :item_id.
Step 2: Check controller parameter usage
The controller incorrectly uses params[:id] which does not exist. It should use params[:order_id] to find the order.
Final Answer:
params[:id] is incorrect; should be params[:order_id] -> Option B
Quick Check:
Parameter names must match route keys [OK]
Hint: Match params keys exactly to route parameter names [OK]
Common Mistakes:
Using generic :id when route uses :order_id
Thinking multiple parameters are not allowed
Confusing controller action names with route parameters
5. You want to create a nested route to show a comment belonging to a post using route parameters. Which route definition correctly captures both post_id and id for the comment?
hard
A. get '/posts/:id/comments/:post_id', to: 'comments#show'
B. get '/posts/comments/:post_id/:id', to: 'comments#show'
C. get '/posts/:post_id/comments/:id', to: 'comments#show'
D. get '/comments/:id/posts/:post_id', to: 'comments#show'
Solution
Step 1: Understand nested resource URL structure
Nested routes usually put the parent resource first, then the child. Here, post_id comes before id for comment.
Step 2: Check route parameter order and names
get '/posts/:post_id/comments/:id', to: 'comments#show' correctly uses /posts/:post_id/comments/:id capturing post's ID and comment's ID in order.
Final Answer:
get '/posts/:post_id/comments/:id', to: 'comments#show' -> Option C
Quick Check:
Parent ID first, child ID second in nested routes [OK]
Hint: Parent resource param before child in nested routes [OK]
Common Mistakes:
Swapping order of post_id and comment id
Putting parameters in wrong positions
Using incorrect parameter names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.