Any web app has routes. A route is a mapping from a url to your ruby code. Each url endpoint can link to part of your ruby code.
Examples of routes can be
/stats/ after your domain name.
If you haven’t created a rails app yet, type
rails new blog.
All Routes for a Rails web app are defined in
/config/routes.rb. By default the file is doesn’t define routes:
You can add new routes here. Lets add one.
This maps the url ‘/hello/‘ to the ruby code (controller) ‘HelloController’. The addition
#index is the method that will be called, which exists even if not defined.
The first word
hello/ is the name of the controller.
Create it with the line:
rails generate controller hello index
Then you’ll have the file
/app/controllers/, with this contents:
class HelloController < ApplicationController
Create a view in
/app/views/hello/. If the directory doesn’t exist, create it. The view file should end in
.erb, but it contains html code.
/app/views/users/ you should have
index.erb with this contents:
Start the app with
Open the link
/hello/ to see the message Hello World.
HelloController, returns the view
If you get the error
Routing error - uninitialized constant
It may be a filename error. The filename must match the class name.
app/controllers/hello_controller.rb should contain