RORGURU.COM
 

INTRODUCTION TO RAILS

 

Rails is a framework which is used for web application. David Heinemeier Hansson is the founder of Rails. Rails follows the MVC architecture and 3-tier architecture. Ruby on Rails supports many web server and database server.

Full Stack Framework:

  1. Includes everything needed to create a database-driven web application using the Model-View-Controller pattern.
  2. Being a full-stack framework means that all layers are built to work seamlessly together.
  3. Requires fewer total lines of code than other frameworks spend setting up their XML configuration files.

Rails Strengths:

Rails is packed with features that make us more productive, with many of the following features building on one other.

Metaprogramming :

       Metaprogramming techniques use programs to write programs. Ruby is one of the best language for metaprogramming, and Rails uses this capability very cleverly. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.

Active Record :

       Rails introduces the Active Record framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to our domain objects using metaprogramming.

Convention over Configuration :

       Rails doesn’t need much configuration where most web development frameworks for .NET or Java force us to write pages of configuration code.

Scaffolding :

       In Rails scaffold method helps us to create CRUD (create, read,update and delete) application very easy.

Built-in Testing :

       Rails creates simple automated tests that can easily be extended . Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all our automated tests with the rake utility.

Three Environments :

       Rails gives you three default environments: development, testing, and production. Each behaves slight differently, making your entire software development cycle easier.

In Rails, there are few webserver supports which are shown below.

* WEBrick
* Mongrel,
* Apache
* Lighttpd etc.

Databaseserver which supported by Rails are shown below:

* MySQL
* SQLite
* SQL server
* DB2
* PostgreSQL etc.

In Rails application, the model holds the information of data and here we can define the relationship between the models. How the data can be represented, view defines it. And the controller is the communicator between model and view.

Command for Rails installation on Windows:

gem install rails --include-dependencies

Command for Rails installation on Mac OS X:

Tsudo gem install rails --include-dependencies

Command for Rails installation on Linux:

gem install rails --include-dependencies

Keeping Rails Up-to-Date

gem update rails

Installation Verification

rails demo

Command to create an application :

Syntax: rails application-name

Example: rails example

When we create a Rails application it automatically creates seventeen folder for different purposes.

Command to create a controller in Rails :

Syntax: ruby script/generate controller controller-name

Example: ruby script/generate controller home index

new show

Creating controller home with index.html.erb ,new html.erb,show html.erb views .

Command to create a model in Rails :

Syntax: ruby script/generate model model-name fieldname:type

Example : ruby script/generate model user name: string age: integer

Command for running an application in Rails:

Syntax: ruby script/server -p 3000

Base Classes :

In Rails every controller ,model and views are inherited from a base class .

For Controller: The base class is Application Controller which is also inherited from the Action Controller.

For Model : Every model in Rails is inherited from the ActiveRecord class .

For View : All views are inherited from Actionview class.

Working with Controller and views :

In Rails, the execution starts from the index method . We can define our own method .

Syntax : class UserController < ApplicationController

def in index

code

end

Example : def index

end

def show

@user = params[:name]

end

end

The corresponding views for this two methods are :


# dex.rhtml :

<% form _tag : action =>'show' do %>

<%=text_field_tag :name %>

<%=submit_tag 'show' %>

<% end %>

# show.rhtml :

<%= @user %>

In the above example we use some tags like :

form_tag : It defines an url . When we are working with many controllers, we can define the controller name and the corresponding action. By default it calls the POST method .
text_field_tag : It creates a text field . When we are using text_field_tag we donot need to write the model name ,we just mention the field name .
submit_tag : It creates a submit button with name show. In Rails there are many methods we use for our application.


In views we can use the following methods:

1) link_to : To link a view to a action in a controller .It acts as a hyperlink .

Example : <%= link_to 'welcome' ,x:action =>'index' %>

2) button_to : Same as the link_to method .Except it creates a button to link a view to a action .

Example : <%= button_to 'home' ,:action =>'index' %>

3) text_area_tag : Creates a text area for longer input .

Example : Enter the student detail<%= text_area_tag :detail %>

4) password_field_tag : It creates a password field . When user enters some character it converts it to a special character .

Example : Enter Password : <%=password field tag : password %>

5) select_tag : Used for creating drop down box. .

Example : <%=select tag "color" , "<option>red</option>,<option>black</option>" %>

6) check_box_tag : Used for creating check box. .

Example : Male <%= check_box_tag 'gender_male' %>

Female <%= check_box_tag 'gender_male' %>

7) radio_button_tag : Used for creating radio button. .

Example : Male <%= radio_button_tag 'gender', male' %>

In controller we can use the following methods :

1) redirect to : It redirects the action to the view after executing the corresponding method . .

Example : redirect_to :action =>index

2) render : It renders the action directly to the view without executing any method . .

Example : render :action =>new