Getting started Ruby on Rails 5

In this post, you will see how to write a rails application from scratch. i.e setup and writing some simple functionality. “Ruby on Rails” is a frame, which comes with a pre-set scaffolding and that courageous MVC pattern as default working principle.

Generate a rails app:

rails new <app-name> -d <database>

Basic Configurations:

If we make changes to Gemfile then run:

bundle install

App-level config:

config/application.rb

# Application configuration should go into files in config/initializers

DB-Configuration: (database.yml)

Query to create a database and user for a rails application.

Login into your database console and execute these queries

CREATE DATABASE test-app_development;

And update database.yml with database name and user/password

Verify DB settings:

rails db:schema:dump

it will generate: db/schema.rb. No output means all good.

Model View Controller:

Model-View-Controller: In a real-life scenario, you can find this pattern very common. As in the cartoon below, is a representation of your favorite burger store. Where the counter manager acts as a Controller, who takes orders from the customer(Brower/Client) and then convey the request to the chef, who start cooking your delicious burger based on your preference. It is similar way controller sends the exchanges the data with the model which is required to display on your view. The Model will be your burger recipe which going to be the same for a respective type of client request. And finally, your controller will get the burger from the kitchen and serve it to the client i.e your View.

MVC: burger store

Let’s discuss the generated app structure.

  • Assets: Images, CSS, javascript
  • Channels: Action Channels
  • Controllers
  • Helpers: View helpers
  • Jobs: Like Cron
  • Mailer: Sending emails
  • Model
  • Views

Start a Web-Server: for development (puma-server)

rails server or rails s

if you get this error: undefined symbol: OPENSSL_init_ssl

//issue with openssl 

Once the puma server started, you can visit the url in browser:

Generate a Controller and a View:

rails g

Please choose a generator below.

Rails:
assets
channel
controller
generator

rails generate controller BurgerController BurgerView

Routing :

Photo by Michał Parzuchowski on Unsplash

Acts like menu options in the burger store. You need a burger, you will choose the burger route. You need french fries, you will pick the respective route.

Types of route:

  • Simple
  • Default
  • Root
  • Resourceful (restful): for rest service

config/routes.rb

Rails.application.routes.draw do

Burger Controller:

class BurgerControllerController < ApplicationController

Burger View (app/views/test_controller/index.html.erb)

<h1>BurgerController#BurgerView</h1>
<p>Find me in app/views/burger_controller/BurgerView.html.erb</p>
Burger View

We have seen how to create a Rails application from scratch with a real-life scenario that resembles the MVC pattern which lays the foundation of this framework.

Thank You for reading it! happy-coding

#redhatter #opensource #developer #kubernetes #keycloak #golang #openshift #quarkus #spring

#redhatter #opensource #developer #kubernetes #keycloak #golang #openshift #quarkus #spring