Wednesday, July 10, 2013

Languages: Ruby on Rails

What is it?

Ruby on Rails is framework for building Web applications. The framework uses the Model-View-Controller (MVC) design pattern and uses convention over configuration when defining the application source structure. For persistence, Rails uses the ActiveRecord pattern and object-relational mapping for storing data. 

Installation

Create an Application

To create an application, change to the directory to where you would like to store the source code. Issue the following command:
> rails new <appname>

The command creates file and folders for the application.

Application Structure

  • Uses models, controls, and views (MVC)
  • Uses routes to map Urls to controller methods

Creating a Controller

> rails generate controller <controller-name-in-plural-form> <nameofview1> <nameofview2>
  • _controller is appended to controller file name
  • derives from ApplicationController which in turn derives from ActionController::Base

Adding an Action to Controller

  • Action is a method inside the controller
  • Method "index" is the default page for a controller
class MyController < ApplicationController
  def index
  end
end

Add a View for the Controller Method

  • A subfolder with the same name as controller is created under view folder to hold views for the controller
  • Name the view file same is as the controller method
  • Named <controllermethodname>.html.erb - contains html, embedded ruby, js, css in the view

Add Routes to the Controller

In config folder, there is routes.rb. Define routes for the controller.

# define routes for controller
resources :<controllername>

7 routes are automatically generated:
  • index
  • show
  • new
  • create (normally, no associated view)
  • edit
  • update (normally, no associated view)
  • destroy (normally, no associated view)

Running the Web Application

From the source folder, run:
> rails server
  • Starts WEBrick server
  • Application is available on the echoed port (Url)
  • Browse to the Url
  • Shows the application page (app development environment can be inspected)
  • To access the application: url/<appName>

Creating a model

rails generate model <controller-name>
  • Creates a migration code that can be run to creates table for the model. Use rake (Ruby's build system) to run the script. rake db:migrate
  • Model file is created under models folder with the same name as the <controllername>
  • <controllername> (singular) can be used to access the table
rails console
  • Starts ruby dev environment
  • Can create database rows
  • Can use <controllername>.new, <controllername>.find, <controllername>.<membername>, <controllername>.save, <controllername>.update_attribute, <controllername>.destroy
<controllername>.update_attributes(:membername => "val", member2name => num)

Controller Actions

class PersonsController < ApplicationController
def index
  @persons= Persons.all
end

def show
end

def new
end

def create
end

def edit
end

def update
end

def destroy
end

end

View

Using embedded Ruby.

<h1>All People</h1>
<%  persons.each do |person| %>
  <p><%= person.name %>
<% end %>