0
0
Ruby on Railsframework~5 mins

Route definition in routes.rb in Ruby on Rails

Choose your learning style9 modes available
Introduction

Routes tell your Rails app how to respond to different web addresses. They connect URLs to the right code that shows pages or handles actions.

When you want to show a list of items on a webpage.
When you need to handle a form submission from a user.
When you want to create a link that shows details about a single item.
When you want to organize your app's URLs in a clear way.
When you want to add a new page or feature to your website.
Syntax
Ruby on Rails
Rails.application.routes.draw do
  get 'url_path', to: 'controller#action'
  post 'url_path', to: 'controller#action'
  resources :resource_name
end

get is for showing pages or data.

post is for sending data, like form submissions.

Examples
This route shows the home page when the user visits '/home'.
Ruby on Rails
get 'home', to: 'pages#home'
This route handles sending a new comment to the server.
Ruby on Rails
post 'comments', to: 'comments#create'
This creates many routes automatically for articles, like showing all, creating, editing, and deleting.
Ruby on Rails
resources :articles
Sample Program

This example sets a route for a welcome page and creates all standard routes for books.

Ruby on Rails
Rails.application.routes.draw do
  get 'welcome', to: 'pages#welcome'
  resources :books
end
OutputSuccess
Important Notes

Routes are checked top to bottom; the first match is used.

Use resources to save time and avoid writing many routes.

Always restart your Rails server after changing routes to see updates.

Summary

Routes connect URLs to controller actions in Rails.

Use get, post, and resources to define routes.

Routes help organize how your app responds to web requests.