text: Ruby on Rails - Quick Guide
  Ruby.
  Ruby on Rails - Installation
  To develop a web application using Ruby on Rails Framework, you need to install the following software −
  - Ruby
  - The Rails Framework
  - A Web Server
  - A Database System.
  Rails Installation on Windows
  Follow the steps given below for installing Ruby on Rails.
  Step 1: Check Ruby Version.
  Step 2: Install.
  Step 3: Install Rails.
  Step 4: Check Rails Version
  Use the following command to check the rails version.
  C:\> rails -v
  Output
  Rails 4.2.4
  Congratulations! You are now on Rails over Windows.
  Rails Installation on Linux.
  Step 1: Install Prerequisite Dependencies
  First of all, we have to install git - core and some ruby dependences that help to install Ruby on Rails. Use the following command for installing Rails dependencies using yum.
  tp>
  Step 2: Install rbenv
  Now we
  Step 3: Install Ruby
  Step 4: Install Rails.
  Step 5: Install JavaScript Runtime.
  Step 6: Install Database
  Keeping Rails Up-to-Date".
  Ruby on Rails - Framework
  A.
  Ruby on Rails MVC Framework
  The Model View Controller principle divides the work of an application into three separate but closely cooperative subsystems.
  Model (ActiveRecord )
  It maintains the relationship between the objects and.
  View ( ActionView )
  It is a presentation of data in a particular format, triggered by a controller's decision to present the data. They are script-based template.
  Controller ( ActionController )).
  Pictorial Representation of MVC Framework
  Given below is a pictorial representation of Ruby on Rails Framework −
  Directory Representation of MVC Framework
  Assuming a standard, default installation over Linux, you can find them like this −
  tp> cd /usr/local/lib/ruby/gems/2.2.0/gems tp> ls
  You will see subdirectories including (but not limited to) the following −
  - actionpack-x.y.z
  - activerecord-x.y.z
  - rails-x.y.z.
  Ruby_8<<.
  Ruby\> Rails server
  It generates the auto code to start the server as shown below −
  _10<<
  What is next?
  The next chapter explains how to create databases for your application and what is the configuration required to access these created databases.
  Further, we will see what Rails Migration is and how it is used to maintain database tables.
  Ruby on Rails - Database Setup
  Before starting with this chapter, make sure your database server is up and running. Ruby on Rails recommends to create three databases - a database each for development, testing, and production environment. According to convention, their names should be −
  - library_development
  - library_production
  - library_test
  You should initialize all three of them and create a user and password for them with full read and write privileges. We are using the root user ID for our application.
  Database Setup for MySQL.
  Configuring database.yml.
  When you finish, it should look something like −
  development: adapter: mysql database: library_development username: root password: [password] host: localhost test: adapter: mysql database: library_test username: root password: [password] host: localhost production: adapter: mysql database: library_production username: root password: [password] host: localhost
  Database Setup for PostgreSQL.
  Configuring database.yml>
  What is Next?
  The next two chapters explain how to model your database tables and how to manage those using Rails Migrations.
  Ruby\> ruby script/generate model Book library\> ruby script/generate model Subject
  Above rails generate model book commands generates the auto code as below −
  .
  Ruby on Rails - Migrations
  Rails.
  What Can Rails Migration Do?
  -.
  Create the Migrations..
  Edit the Code.
  Run the Migration.
  Running Migrations for Production and Test Databases.
  What is Next?
  Now we have our database and the required tables available. In the two subsequent chapters, we will explore two important components called Controller (ActionController) and View (ActionView).
  Creating Controllers (Action Controller).
  Creating Views (Action View).
  Ruby on Rails - Controller
  The
  class BookController < ApplicationController end @book =.
  Implementing the new Method
  The new method lets Rails know that you will create a new object. So just add the following code in this method.
  def new @book = Book.new @subjects = Subject.all end
  The above method will be called when you will display a page to the user to take user input. Here second line grabs all the subjects from the database and puts them in an array called @subjects.
  Implementing the create Method
  Once you take user input using HTML. So, you can create a method inside book_controller.rb to display all the subjects. Assume.
  Ruby.
  Ruby_14<<_15<<_16<<_17<<_18<<_20<<
  What is Next?
  Hope now you are feeling comfortable with all the operations of Rails.
  The next chapter explains how to use Layouts to put your data in a better way. We will show you how to use CSS in your Rails applications.
  Ruby.
  Ruby on Rails - Scaffolding
  While you're developing Rails applications, especially those which are mainly providing you with a simple interface to data in a database, it can often be useful to use the scaffold method.
  Scaffolding provides more than cheap demo thrills. Here are some benefits −
  You can quickly get code in front of your users for feedback.
  You are motivated by faster success.
  You can learn how Rails works by looking at the generated code.
  You can use scaffolding as a foundation to jump start your development.
  Scaffolding Example
  To understand scaffolding, let's create a database called cookbook and a table called recipes.
  Creating an Empty Rails Web Application
  Open a command window and navigate to where you want to create this cookbook web application. So, run the following command to create a complete directory structure.
  tp> rails new cookbook
  Setting up the Database
  Here is the way to create a database −
  mysql> create database cookbook; Query OK, 1 row affected (0.01 sec) mysql> grant all privileges on cookbook.* to 'root'@'localhost' identified by 'password'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
  To instruct Rails how to find the database, edit the configuration file cookbook\config\database.yml and change the database name to cookbook. Leave the password empty. When you finish, it should look as follows −
  development: adapter: mysql database: cookbook username: root password: [password] host: localhost test: adapter: mysql database: cookbook username: root password: [password] host: localhost production: adapter: mysql database: cookbook username: root password: [password] host: localhost
  Rails lets you run in the development mode, test mode, or production mode, using different databases. This application uses the same database for each.
  The Generated Scaffold Code the scaffold helper script −
  cookbook> rails generate scaffold recipe
  It generates auto-files as shown below −
  The Controller
  Let's look at the code behind the controller. This code is generated by the scaffold generator. If you open app/controllers/recipes_controller.rb, then you will find something as follows −
  class RecipesController < ApplicationController before_action :set_recipe, only: [:show, :edit, :update, :destroy] # GET /recipes # GET /recipes.json def index @recipes = Recipe.all end # GET /recipes/1 # GET /recipes/1.json def show end # GET /recipes/new def new @recipe = Recipe.new end # GET /recipes/1/edit def edit end # POST /recipes # POST /recipes.json def create @recipe = Recipe.new(recipe_params) respond_to do |format| if @recipe.save format.html { redirect_to @recipe, notice: 'Recipe was successfully created.' } format.json { render :show, status: :created, location: @recipe } else format.html { render :new } format.json { render json: @recipe.errors, status: :unprocessable_entity } end end end # PATCH/PUT /recipes/1 # PATCH/PUT /recipes/1.json def update respond_to do |format| if @recipe.update(recipe_params) format.html { redirect_to @recipe, notice: 'Recipe was successfully updated.' } format.json { render :show, status: :ok, location: @recipe } else format.html { render :edit } format.json { render json: @recipe.errors, status: :unprocessable_entity } end end end # DELETE /recipes/1 # DELETE /recipes/1.json def destroy @recipe.destroy respond_to do |format| format.html { redirect_to recipes_url, notice: 'Recipe was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_recipe @recipe = Recipe.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def recipe_params params.require(:recipe).permit(:tittle, :instructions) end end
  When the user of a Rails application selects an action, e.g. "Show" - the controller will execute any code in the appropriate section - "def show" - and then by default will render a template of the same name - "show.html.erb"..
  This single line of code will bring the database table to life. It will provide with a simple interface to your data, and ways of −
  - Creating new entries
  - Editing current entries
  - Viewing current entries
  - Destroying current entries
  When creating or editing an entry, scaffold will do all the hard work like form generation and handling for you, and will even provide clever form generation, supporting the following types of inputs −
  - Simple text strings
  - Text areas (or large blocks of text)
  - Date selectors
  - Date-time selectors
  You can use Rails Migrations to create and maintain tables.
  rake db:migrate RAILS_ENV=development
  Now, go to the cookbook directory and run the Web Server using the following command −
  cookbook> rails server
  Now, open a browser and navigate to. This will provide you a screen to create new entries in the recipes table. A screenshot is shown below −
  Once you press the Create button to create a new recipe, your record is added into the recipes table and it shows the following result −
  You can see the option to edit, show, and destroy the records. So, play around with these options.
  You can also list down all the recipes available in the recipes table using the URL.
  Enhancing the Model
  Rails gives you a lot of error handling for free. To understand this, add some validation rules to the empty recipe model −
  Modify app/models/recipe.rb as follows and then test your application −
  class Recipe < ActiveRecord::Base validates_length_of :title, :within => 1..20 validates_uniqueness_of :title, :message => "already exists" end
  These entries will give automatic checking.
  validates_length_of − the field is not blank and not too long.
  validates_uniqueness_of − duplicate values are trapped. Instead of the default Rails error message, we have given a custom message here.
  Alternative Way to Create Scaffolding
  Create an application as shown above and The Generated Scaffold Code as shown below
  rails g scaffold Recipe tittle:string instructions:text
  Above code generates the auto files with data base by using with sqlite3 with tittle and instruction column as shown below an image.
  we need to migrate the data base by using below syntax.
  $ rake db:migrate RAILS_ENV=development
  Finally run the application by using the following command line −
  rails server
  It will generate the result as shown above output images.
  The Views
  All the views and corresponding all the controller methods are created by scaffold command and they are available in the app/views/recipes directory.
  How Scaffolding is Different?
  If you have gone through the previous chapters, then you must have seen that we had created methods to list, show, delete and create data etc., but scaffolding does that job automatically.
  Ruby on Rails - AJAX
  Ajax enables you to retrieve data for a web page without having to refresh the contents of the entire page. In the basic web architecture, the user clicks a link or submits a form. The form is submitted to the server, which then sends back a response. The response is then displayed for the user on a new page.
  When you interact with an Ajax-powered web page, it loads an Ajax engine in the background. The engine is written in JavaScript and its responsibility is to both communicate with the web server and display the results to the user. When you submit data using an Ajax-powered form, the server returns an HTML fragment that contains the server's response and displays only the data that is new or changed as opposed to refreshing the entire page.
  For a complete detail on AJAX you can go through our AJAX Tutorial The client-side JavaScript, which Rails creates automatically, receives the HTML fragment and uses it to update a specified part of the current page's HTML, often the content of a <div>.
  AJAX Example
  This example works based on scaffold, Destroy concept works based on ajax.
  In this example, we will provide, list, show and create operations on ponies table. If you did not understand the scaffold technology then we would suggest you to go through the previous chapters first and then continue with AJAX on Rails.
  Creating An Application
  Let us start with the creation of an application It will be done as follows −
  rails new ponies
  The above command creates an application, now we need to call the app directory using with cd command. It will enter in to an application directory then we need to call a scaffold command. It will be done as follows −
  rails generate scaffold Pony name:string profession:string
  Above command generates the scaffold with name and profession column. We need to migrate the data base as follows command
  rake db:migrate
  Now Run the Rails application as follows command
  rails s
  Now open the web browser and call a url as, The output will be as follows
  Creating an Ajax
  Now open app/views/ponies/index.html.erb with suitable text editors. Update your destroy line with :remote => true, :class => 'delete_pony'.At finally, it looks like as follows.
  Create a file, destroy.js.erb, put it next to your other .erb files (under app/views/ponies). It should look like this −
  Now enter the code as shown below in destroy.js.erb
  $('.delete_pony').bind('ajax:success', function() { $(this).closest('tr').fadeOut(); });
  Now Open your controller file which is placed at app/controllers/ponies_controller.rb and add the following code in destroy method as shown below −
  # DELETE /ponies/1 # DELETE /ponies/1.json def destroy @pony = Pony.find(params[:id]) @pony.destroy respond_to do |format| format.html { redirect_to ponies_url } format.json { head :no_content } format.js { render :layout => false } end end
  At finally controller page is as shown image.
  Now run an application, Output called from, it will looks like as following image
  Press on create pony button, it will generate the result as follows
  Now click on back button, it will show all pony created information as shown image
  Till now, we are working on scaffold, now click on destroy button, it will call a pop-up as shown below image, the pop-up works based on Ajax.
  If Click on ok button, it will delete the record from pony. Here I have clicked ok button. Final output will be as follows −
  Ruby_36<<_37<<_38<<
  For a complete detail on File object, you need to go through the Ruby Reference Manual.
  Ruby on Rails - Send Emails
  Action: '<username>', password: '\mail.