0
0
Ruby on Railsframework~20 mins

Route definition in routes.rb in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Route definition in routes.rb
📖 Scenario: You are building a simple blog application using Ruby on Rails. You need to define routes so that users can visit pages to see all posts, view a single post, create a new post, and edit existing posts.
🎯 Goal: Define routes in the config/routes.rb file to connect URL paths to controller actions for posts.
📋 What You'll Learn
Create a root route that points to the posts#index action
Define a route for GET /posts to list all posts
Define a route for GET /posts/:id to show a single post
Define a route for GET /posts/new to show the new post form
Define a route for GET /posts/:id/edit to show the edit form
💡 Why This Matters
🌍 Real World
Defining routes is essential in Rails apps to connect URLs to the code that handles requests, like showing pages or submitting forms.
💼 Career
Rails developers must know how to configure routes to build web applications that respond correctly to user navigation and actions.
Progress0 / 4 steps
1
Set up the root route
In the config/routes.rb file, write a line to set the root route to the posts#index action.
Ruby on Rails
Need a hint?

The root route uses the root method followed by the controller and action in quotes.

2
Add route for listing all posts
Add a route for GET /posts that maps to the posts#index action using the get method.
Ruby on Rails
Need a hint?

Use get '/posts', to: 'posts#index' to define this route.

3
Add routes for showing, creating, and editing posts
Add routes for these paths and actions using get:
  • GET /posts/:id to posts#show
  • GET /posts/new to posts#new
  • GET /posts/:id/edit to posts#edit
Ruby on Rails
Need a hint?

Use get with the path and to: option for each route.

4
Complete the routes file
Ensure the config/routes.rb file has all the routes inside the Rails.application.routes.draw do ... end block exactly as specified.
Ruby on Rails
Need a hint?

Make sure all routes are inside the Rails.application.routes.draw do ... end block.