Building A Hello World App In Ruby On Rails App

Published:
Last modified:
Tags Ror , Rails , Web-Development

Installing Rails

Using the RubyGems package manager, we can install all rails dependencies with one command.


$ sudo gem install rails -v 5.0.0
...
29 gems installed

_-v_ specify Rails version to install, in this case: 5.0.0

Create the rails app structure

Using rails new command creates the skeleton for each rails app.


$ mkdir rails_helloworld
$ cd rails_helloworld
rails_helloworld$ rails _5.0.0_ new hello_app
      create  
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/config/manifest.js
      create  app/assets/javascripts/application.js
      create  app/assets/javascripts/cable.js
      create  app/assets/stylesheets/application.css
      create  app/channels/application_cable/channel.rb
      create  app/channels/application_cable/connection.rb
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/jobs/application_job.rb
      create  app/mailers/application_mailer.rb
      create  app/models/application_record.rb
      create  app/views/layouts/application.html.erb
      create  app/views/layouts/mailer.html.erb
      create  app/views/layouts/mailer.text.erb
      create  app/assets/images/.keep
      create  app/assets/javascripts/channels
      create  app/assets/javascripts/channels/.keep
      create  app/controllers/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  bin/setup
      create  bin/update
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/secrets.yml
      create  config/cable.yml
      create  config/puma.rb
      create  config/spring.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/application_controller_renderer.rb
      create  config/initializers/assets.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/cookies_serializer.rb
      create  config/initializers/cors.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_framework_defaults.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/apple-touch-icon-precomposed.png
      create  public/apple-touch-icon.png
      create  public/favicon.ico
      create  public/robots.txt
      create  test/fixtures
      create  test/fixtures/.keep
      create  test/fixtures/files
      create  test/fixtures/files/.keep
      create  test/controllers
      create  test/controllers/.keep
      create  test/mailers
      create  test/mailers/.keep
      create  test/models
      create  test/models/.keep
      create  test/helpers
      create  test/helpers/.keep
      create  test/integration
      create  test/integration/.keep
      create  test/test_helper.rb
      create  tmp
      create  tmp/.keep
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.keep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.keep
      remove  config/initializers/cors.rb
         run  bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies.................................................
Using rake 11.2.2
Using concurrent-ruby 1.0.2
Using i18n 0.7.0
Using minitest 5.9.0
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.1.0
Using pkg-config 1.1.7
Using rack 2.0.1
Using nio4r 1.2.1
Using websocket-extensions 0.1.2
Using mime-types-data 3.2016.0521
Using arel 7.1.1
Using bundler 1.11.2
Using byebug 9.0.5
Using coffee-script-source 1.10.0
Using execjs 2.7.0
Using method_source 0.8.2
Using thor 0.19.1
Using debug_inspector 0.0.2
Using ffi 1.9.14
Using multi_json 1.12.1
Using rb-fsevent 0.9.7
Using puma 3.6.0
Using sass 3.4.22
Using tilt 2.0.5
Using spring 1.7.2
Using sqlite3 1.3.11
Using turbolinks-source 5.0.0
Using tzinfo 1.2.2
Using nokogiri 1.6.8
Using rack-test 0.6.3
Using sprockets 3.7.0
Using websocket-driver 0.6.4
Using mime-types 3.1
Using coffee-script 2.4.1
Using uglifier 3.0.1
Using rb-inotify 0.9.7
Installing turbolinks 5.0.1
Using activesupport 5.0.0
Using loofah 2.0.3
Using mail 2.6.4
Using listen 3.0.8
Using rails-dom-testing 2.0.1
Using globalid 0.3.7
Using activemodel 5.0.0
Installing jbuilder 2.6.0
Using rails-html-sanitizer 1.0.3
Installing spring-watcher-listen 2.0.0
Using activejob 5.0.0
Using activerecord 5.0.0
Using actionview 5.0.0
Using actionpack 5.0.0
Using actioncable 5.0.0
Using actionmailer 5.0.0
Using railties 5.0.0
Using sprockets-rails 3.1.1
Installing coffee-rails 4.2.1
Installing jquery-rails 4.1.1
Installing web-console 3.3.1
Using rails 5.0.0
Installing sass-rails 5.0.6
Bundle complete! 15 Gemfile dependencies, 63 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
         run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted

The command rails new automatically runs bundle install after creating the following directories and files:

.
└── hello_app
    β”œβ”€β”€ app
    β”‚Β Β  β”œβ”€β”€ assets
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ config
    β”‚Β Β  β”‚Β Β  β”‚Β Β  └── manifest.js
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ images
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ javascripts
    β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ application.js
    β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ cable.js
    β”‚Β Β  β”‚Β Β  β”‚Β Β  └── channels
    β”‚Β Β  β”‚Β Β  └── stylesheets
    β”‚Β Β  β”‚Β Β      └── application.css
    β”‚Β Β  β”œβ”€β”€ channels
    β”‚Β Β  β”‚Β Β  └── application_cable
    β”‚Β Β  β”‚Β Β      β”œβ”€β”€ channel.rb
    β”‚Β Β  β”‚Β Β      └── connection.rb
    β”‚Β Β  β”œβ”€β”€ controllers
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ application_controller.rb
    β”‚Β Β  β”‚Β Β  └── concerns
    β”‚Β Β  β”œβ”€β”€ helpers
    β”‚Β Β  β”‚Β Β  └── application_helper.rb
    β”‚Β Β  β”œβ”€β”€ jobs
    β”‚Β Β  β”‚Β Β  └── application_job.rb
    β”‚Β Β  β”œβ”€β”€ mailers
    β”‚Β Β  β”‚Β Β  └── application_mailer.rb
    β”‚Β Β  β”œβ”€β”€ models
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ application_record.rb
    β”‚Β Β  β”‚Β Β  └── concerns
    β”‚Β Β  └── views
    β”‚Β Β      └── layouts
    β”‚Β Β          β”œβ”€β”€ application.html.erb
    β”‚Β Β          β”œβ”€β”€ mailer.html.erb
    β”‚Β Β          └── mailer.text.erb
    β”œβ”€β”€ bin
    β”‚Β Β  β”œβ”€β”€ bundle
    β”‚Β Β  β”œβ”€β”€ rails
    β”‚Β Β  β”œβ”€β”€ rake
    β”‚Β Β  β”œβ”€β”€ setup
    β”‚Β Β  β”œβ”€β”€ spring
    β”‚Β Β  └── update
    β”œβ”€β”€ config
    β”‚Β Β  β”œβ”€β”€ application.rb
    β”‚Β Β  β”œβ”€β”€ boot.rb
    β”‚Β Β  β”œβ”€β”€ cable.yml
    β”‚Β Β  β”œβ”€β”€ database.yml
    β”‚Β Β  β”œβ”€β”€ environment.rb
    β”‚Β Β  β”œβ”€β”€ environments
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ development.rb
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ production.rb
    β”‚Β Β  β”‚Β Β  └── test.rb
    β”‚Β Β  β”œβ”€β”€ initializers
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ application_controller_renderer.rb
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ assets.rb
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ backtrace_silencers.rb
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ cookies_serializer.rb
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ filter_parameter_logging.rb
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ inflections.rb
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ mime_types.rb
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ new_framework_defaults.rb
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ session_store.rb
    β”‚Β Β  β”‚Β Β  └── wrap_parameters.rb
    β”‚Β Β  β”œβ”€β”€ locales
    β”‚Β Β  β”‚Β Β  └── en.yml
    β”‚Β Β  β”œβ”€β”€ puma.rb
    β”‚Β Β  β”œβ”€β”€ routes.rb
    β”‚Β Β  β”œβ”€β”€ secrets.yml
    β”‚Β Β  └── spring.rb
    β”œβ”€β”€ config.ru
    β”œβ”€β”€ db
    β”‚Β Β  └── seeds.rb
    β”œβ”€β”€ Gemfile
    β”œβ”€β”€ Gemfile.lock
    β”œβ”€β”€ lib
    β”‚Β Β  β”œβ”€β”€ assets
    β”‚Β Β  └── tasks
    β”œβ”€β”€ log
    β”œβ”€β”€ public
    β”‚Β Β  β”œβ”€β”€ 404.html
    β”‚Β Β  β”œβ”€β”€ 422.html
    β”‚Β Β  β”œβ”€β”€ 500.html
    β”‚Β Β  β”œβ”€β”€ apple-touch-icon.png
    β”‚Β Β  β”œβ”€β”€ apple-touch-icon-precomposed.png
    β”‚Β Β  β”œβ”€β”€ favicon.ico
    β”‚Β Β  └── robots.txt
    β”œβ”€β”€ Rakefile
    β”œβ”€β”€ README.md
    β”œβ”€β”€ test
    β”‚Β Β  β”œβ”€β”€ controllers
    β”‚Β Β  β”œβ”€β”€ fixtures
    β”‚Β Β  β”‚Β Β  └── files
    β”‚Β Β  β”œβ”€β”€ helpers
    β”‚Β Β  β”œβ”€β”€ integration
    β”‚Β Β  β”œβ”€β”€ mailers
    β”‚Β Β  β”œβ”€β”€ models
    β”‚Β Β  └── test_helper.rb
    β”œβ”€β”€ tmp
    β”‚Β Β  └── cache
    β”‚Β Β      └── assets
    └── vendor
        └── assets
            β”œβ”€β”€ javascripts
            └── stylesheets

45 directories, 57 files

bundle install reads the content of ruby_helloworld/Gemfile and install each dependency.

Serve pages in development

Running rails server launches a server for development purposes on http://localhost:3000


rails_helloworld$ rails server
=> Booting Puma
=> Rails 5.0.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

Opening the address http://localhost:3000 will show up rails homescreen:

rails homescreen

Started GET "/" for 127.0.0.1 at 2016-08-07 18:58:25 -0300
Processing by Rails::WelcomeController#index as HTML
  Parameters: {"internal"=>true}
  Rendering /var/lib/gems/2.3.0/gems/railties-5.0.0/lib/rails/templates/rails/welcome/index.html.erb
  Rendered /var/lib/gems/2.3.0/gems/railties-5.0.0/lib/rails/templates/rails/welcome/index.html.erb (8.7ms)
Completed 200 OK in 41ms (Views: 18.7ms | ActiveRecord: 0.0ms)

Creating the Hello World page

Routing

Edit the controller app/controllers/routes.rb that handles the homepage with the action hello to retrieve Hello World.

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def hello
    render html: "hello, world!"
  end
end

Adding controller action

Add the route that will handle the access to the / url with root:controller#action.

Rails.application.routes.draw do
  root 'application#hello'
end
Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·