Fristly, we should choose a right OS. I suggest ubuntu 20.04 which is very easy for rails development.

Then we should have ruby installed. Rails7 requires ruby 2.7 and above.

And we need to have sqlite3 installed on the system. Just run 'apt install sqlite3' to install it.

After then we can install rails by this command:

 $ sudo gem install rails

We show all the software versions installed:

 $ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]
$ sqlite3 --version
3.31.1 2020-01-27 19:55:54 3bfa9cc97da10598521b342961df8f5f68c7388fa117345eeb516eaa837balt1
$ rails --version
Rails 7.0.4

Let's create a project space now. The command:

 $ rails new blog

Then 'cd blog' to switch into the new dir, and run the following command to see the structure.

 $ tree -d -L 2
.
├── app
│   ├── assets
│   ├── channels
│   ├── controllers
│   ├── helpers
│   ├── javascript
│   ├── jobs
│   ├── mailers
│   ├── models
│   └── views
├── bin
├── config
│   ├── environments
│   ├── initializers
│   └── locales
├── db
├── lib
│   ├── assets
│   └── tasks
├── log
├── public
├── storage
├── test
│   ├── channels
│   ├── controllers
│   ├── fixtures
│   ├── helpers
│   ├── integration
│   ├── mailers
│   ├── models
│   └── system
├── tmp
│   ├── cache
│   ├── pids
│   ├── sockets
│   └── storage
└── vendor
└── javascript

As you see above, the child dir "app" and "config" are important for our new development.

Given your hostname is "www.sample.com", add this entry to "config/environments/development.rb":

   config.hosts << "www.sample.com"

And start rails server by:

 $ bin/rails server -b 0.0.0.0

Now, you can open a browser to access the new project by this URL:

 http://www.sample.com:3000/

What you see is the default rails page. But we could add a bit new content to the project page.

Since rails is MVC driven, we need to create a customized route at the first.

Edit "config/routes.rb" and add this line:

   get "/articles", to: "articles#index"

The route above declares that GET /articles requests are mapped to the index action of ArticlesController.

We have to create the new controller for now, by running this command:

 $ bin/rails generate controller Articles index --skip-routes

The controller "app/controllers/articles_controller.rb" has been created. Let's take a look at it:

 class ArticlesController < ApplicationController
def index
end
end

So we have created route, controller, but what's the view here?

The index action is empty. When an action does not explicitly render a view (or otherwise trigger an HTTP response), Rails will automatically render a view that matches the name of the controller and action. Convention Over Configuration! Views are located in the app/views directory. So the index action will render app/views/articles/index.html.erb by default.

Let's open app/views/articles/index.html.erb, and replace its contents with:

 <h1>Hello, Rails!</h1>

Now we can see the new change by the URL:

 http://www.sample.com:3000/articles

Let's re-edit "config/routes.rb" and change it to:

 Rails.application.routes.draw do
root "articles#index"
get "/articles", to: "articles#index"
end

So we can see our "Hello, Rails!" text when we visit http://www.sample.com:3000, confirming that the root route is also mapped to the index action of ArticlesController.

Return to home | Generated on 09/29/22