[Rails] Route

Tags
Rails
Engineering
Created
Oct 19, 2023 02:33 AM
Edited
Oct 18, 2023
Description
Very basic Rails route concept

What is the Rails route?

The Rails router recognizes URLs and dispatches them to a controller's action, or to a Rack application.

How to use


# route code
get '/patients/:id', to: 'patients#show', as: 'patient'

# controller code
@patient = Patient.find(params[:id])

# view code
<%= link_to 'Patient Record', patient_path(@patient) %>
 
Inside config/routes.rb
Rails.application.routes.draw do
   root 'book#index'
	 get 'book/list'
   get 'book/new'
   post 'book/create'
   patch 'book/update'
   get 'book/list'
   get 'book/show'
   get 'book/edit'
   get 'book/delete'
   get 'book/update'
   get 'book/show_subjects'
end

# by resource
Rails.application.routes.draw do
  resources :brands, only: [:index, :show] do
    resources :products, only: [:index, :show]
  end

  resource :basket, only: [:show, :update, :destroy]

  resolve("Basket") { route_for(:basket) }
end

Source