Create a new rails app hello-world
from command in Windows and Terminal in Linux.
rails new hello-world
Now move to new app directory
cd hello-world
Now generate a controller
rails generate controller hello_world index
Here index
is the name of method in hello_world
controller. You can check it opening the file app/controllers/hello_world_controller.rb
in your application directory. Code looks like this:
class HelloWorldController < ApplicationController
def index
end
end
A route
is automatically added in your config/routes.rb
file which points to your method. See the code in your routes.rb
file.
Rails.application.routes.draw do
get 'hello_world/index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Now open file app/views/hello_world/index.html.rb
Clear all the content and write
Hello, this is my first rails page.
Start rails server:
rails server
Open this url in your browser:
http://localhost:3000/hello_world/
You should see:
Hello, this is my first rails page
Make your new page, your home page. In routes.rb file in config folder remove the line get 'hello_world/index'
and add:
root 'hello_world#index'
Now open: http://localhost:3000/
You will see: Hello, this is my first rails
You are done.