Routing
Writing urls
config/routes.rb
Rails.application.routes.draw do
get 'welcome/home', to: 'welcome#home'
get 'welcome/about', to: 'welcome#about'
end
Creating controllers
create new file
app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
def home
end
def about
end
end
Creating a view
app/views/welcome/home.html.erb
<h1>Home Page</h1>
<%= link_to 'About', welcome_about_path %>
app/views/welcome/about.html.erb
<h1>About Page</h1>
<%= link_to 'Home', welcome_home_path %>
For to create link to a page, run
rake routes
commandNotice
welcome_home
inPrefix
column. append_path
to it. like thiswelcome_home_path
List all routs (All defined urls)
rake routes
Output
$ rake routes
Prefix Verb URI Pattern Controller#Action
welcome_home GET /welcome/home(.:format) welcome#home
welcome_about GET /welcome/about(.:format) welcome#about
Creating index/landing page url
We will modify above example
config/routes.rb
Rails.application.routes.draw do
root to: 'welcome#home'
get 'about', to: 'welcome#about'
end
app/views/welcome/home.html.erb
<h1>Home Page</h1>
<%= link_to 'About', about_path %>
app/views/welcome/about.html.erb
<h1>About Page</h1>
<%= link_to 'Home', root_path %>