We are living as nomad in Cyprus !!
ruby on rails

Install Ruby on Rails

How to install Ruby on Rails. Rails a web application development framework written in the Ruby programming language.

Contents

Install Ruby

Before installing Rails, need to install Ruby at first.

Terminal

$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]

If you don't install Ruby, see the following article.

Ruby

Install Ruby from source

Update the latest Gem

Keep your gem up to date, just in case.

Terminal

$ gem update --system

Install bundler

It is better to install the Bundler gem, because it helps to manage your application dependencies. Use the following command to install bundler gem.

Terminal

$ gem install bundler
Fetching bundler-2.1.4.gem
Successfully installed bundler-2.1.4
Parsing documentation for bundler-2.1.4
Installing ri documentation for bundler-2.1.4
Done installing documentation for bundler after 5 seconds
1 gem installed

Install Rails

Use gem command to install Rails.

This time, rails application path is "/path_to/ruby_workplace".

Terminal

$ gem install rails

Next, create a new rails application "demo".

Terminal

$ cd /path_to/ruby_workplace
$ rails new demo  // Create a new rails application "demo".
$ ls -l
-rw-r--r--    1 noknow  staff    1976  4  9 03:06 Gemfile
-rw-r--r--    1 noknow  staff    5551  4  9 03:06 Gemfile.lock
-rw-r--r--    1 noknow  staff     374  4  9 03:06 README.md
-rw-r--r--    1 noknow  staff     227  4  9 03:06 Rakefile
drwxr-xr-x   11 noknow  staff     352  4  9 03:06 app
-rw-r--r--    1 noknow  staff    1722  4  9 03:06 babel.config.js
drwxr-xr-x   10 noknow  staff     320  4  9 03:06 bin
drwxr-xr-x   18 noknow  staff     576  4  9 03:06 config
-rw-r--r--    1 noknow  staff     130  4  9 03:06 config.ru
drwxr-xr-x    3 noknow  staff      96  4  9 03:06 db
drwxr-xr-x    4 noknow  staff     128  4  9 03:06 lib
drwxr-xr-x    4 noknow  staff     128  4  9 03:06 log
drwxr-xr-x  778 noknow  staff   24896  4  9 03:08 node_modules
-rw-r--r--    1 noknow  staff     313  4  9 03:08 package.json
-rw-r--r--    1 noknow  staff     224  4  9 03:06 postcss.config.js
drwxr-xr-x    9 noknow  staff     288  4  9 03:06 public
drwxr-xr-x    3 noknow  staff      96  4  9 03:06 storage
drwxr-xr-x   12 noknow  staff     384  4  9 03:06 test
drwxr-xr-x    7 noknow  staff     224  4  9 03:06 tmp
drwxr-xr-x    3 noknow  staff      96  4  9 03:06 vendor
-rw-r--r--    1 noknow  staff  327041  4  9 03:08 yarn.lock
$ cd demo
$ rails server  // Start rails application.
$ curl -o /dev/null --dump-header - http://127.0.0.1:3000/ -s
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: strict-origin-when-cross-origin
Content-Type: text/html; charset=utf-8
ETag: W/"ff93c7a40702185696f08d33e047103d"
Cache-Control: max-age=0, private, must-revalidate
Content-Security-Policy: script-src 'unsafe-inline'; style-src 'unsafe-inline'
X-Request-Id: 531055f1-4bb7-48f2-a1c1-25ec7f4075a3
X-Runtime: 0.006163
Transfer-Encoding: chunked

Started to rails application successfully.

Once stopping rails application, try to develop to respond "Hello World"

Respond "Hello World"

When to access the root URL, Make "Hello World" respond.

Firstly, create a controller.

Terminal

$ cd /ruby_workplace_path/demo  // Move to the path of rails app "demo".
$ rails generate controller Welcome index
Running via Spring preloader in process 85960
      create  app/controllers/welcome_controller.rb
       route  get 'welcome/index'
      invoke  erb
      create    app/views/welcome
      create    app/views/welcome/index.html.erb
      invoke  test_unit
      create    test/controllers/welcome_controller_test.rb
      invoke  helper
      create    app/helpers/welcome_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    scss
      create      app/assets/stylesheets/welcome.scss

Have a look the following files which was created by rails generate controller command.

- app/controllers/welcome_controller.rb

- app/views/welcome/index.html.erb

welcome_controller.rb

class WelcomeController < ApplicationController
  def index
    # Default: render "welcome/index", so nothing to do is OK.
  end
end

Edit "Hello World" on app/views/welcome/index.html.erb file.

index.html.erb

<h1>Hello Wrold</h1>

Modify the routing file which is "config/routes.rb" to respond for root access.

config/routes.rb

Rails.application.routes.draw do
  root 'welcome#index'  // Setting rout access.
  get 'welcome/index'  // Added it automatically by rails generate controller command.

Start to rails application again and then confirm it.

Terminal

$ rails server
$ curl 127.0.0.1:3000
...
<h1>Hello Wrold</h1>
...

Error handling

Node.js not installed...

error

sh: node: command not found
sh: nodejs: command not found
Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/

Need to install Node.js.

Nodejs

Install Nodejs from source

Yarn not installed...

error

Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/

Need to install Yarn.

Yarn

Install Yarn from source

Search by keywords

Select Language

/

Follow us by