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>rails new test-app -d mysql (default:sqllite)
If we make changes to Gemfile then run:
# Application configuration should go into files in config/initializersi.e :config/initializers
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;CREATE DATABASE test-app_test;GRANT ALL PRIVILEGES ON test-app_development.* To 'rails_user'@'localhost' IDENTIFIED By '<password>';GRANT ALL PRIVILEGES ON test-app_test.* To 'rails_user'@'localhost' IDENTIFIED By '<password>';
And update database.yml with database name and user/password
Verify DB settings:
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.
Let’s discuss the generated app structure.
- Channels: Action Channels
- Helpers: View helpers
- Jobs: Like Cron
- Mailer: Sending emails
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 gem install puma -- --with-opt-dir=/usr/local/bin/openssl (location of openssl installation)
Once the puma server started, you can visit the url in browser:
Generate a Controller and a View:
Please choose a generator below.
rails generate controller BurgerController BurgerView
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:
- Resourceful (restful): for rest service
Rails.application.routes.draw doget 'burger_controller/BurgerView'# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.htmlend
class BurgerControllerController < ApplicationControllerlayout falsedef BurgerView
@burgerList = ['Cheese-Burger', 'Bacon Burgers', 'Green-Chile Burgers']
Burger View (app/views/test_controller/index.html.erb)
<p>Find me in app/views/burger_controller/BurgerView.html.erb</p><% @burgerList.each do |n| %>
<%= n %> <br />
<% end %>
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