RubyProject[1] - Wikispaces

pityknockInternet and Web Development

Feb 2, 2013 (3 years and 10 months ago)

217 views



TECHNOLOGY PROJECT ON


RUBY ON RAILS


(Project report submitted for course CS 612 Concepts and Structure In
Internet Computing)






SUBMITTED TO


Dr. Christelle Scharff

Department of Computer Science and Information Systems

P
ace

U
niversity

N
ew

Y
ork

C
ity




SUBMITTED BY


Alina Joshi

Geetu Aggarwal

Kirandeep Brar

Mounika Ma
d
g
u
l
a

Sushma Shivaramaiah


http://cs612rubyonrails.wikispaces.com/


Date: 3
rd

December 2007





Table Of Contents


1.

Introduction

1.1

Ru
by

1.2

Rails

1.3

Model View Architecture

1.3.1

Model

1.3.2

View

1.3.3

Controller

2.

Installing the Software

2.1

Direct Procedure for Download

2.2

Install and Verify

3.

Development of Web Application

3.1

Step 1 : Creating an Empty Rails Web Application

3.2

Step 2 : Testing the Empty Web Application

3.3

A Ra
ils Application's Directory Structure

3.4

Step 3 Database

3.4.1

Create the tables your app will need to produce that feature

3.5

Step 4 : Generate a code base to use the table(s)

3.6 Modifying the colors and fonts in stylesheets

4.

References

5.

Appendix

5.1

Appendix I:
Flow
of Application

5.2

Appendix I
I
: Code

5.3

Appendix II
I
: ScreenShots of AddressBook Application








1.Introduction

Ruby on Rails

is a free web application framework It aims to increase the speed and ease
with which database
-
driven web sites can be created, and

offers skeleton code
frameworks scaffolding from the outset. Ruby on Rails is an open source project written
in the Ruby programming language and applications using the Rails framework that are
developed using the Model
-
View
-
Controller paradigm.

1.1 Rub
y

Ruby

is a pure object
-
oriented
programming language

with a super clean syntax that
makes programming elegant and fun. Ruby successfully combines Smalltalk's conceptual
elegance, Python's ease of use and learning, and Perl's pragmatism. Ruby originated in

Japan in the early 1990s, and has started to become popular worldwide in the past few
years as more English language books and documentation have become available.

1.2 Rails

Rails

is an open source Ruby framework for developing database
-
backed web
applica
tions. With this one can develop a web application
at least

ten times faster as
compared to a typical Java framework

1.3 Model View Architecture

Model
-
view
-
controller

(
MVC
) is an architectural pattern used in software engineering.
In complex computer appl
ications that present a large amount of data to the user, a
developer often wishes to separate data (model) and user interface (view) concerns, so
that changes to the user interface will not affect data handling, and that the data can be
reorganized withou
t changing the user interface. The model
-
view
-
controller solves this
problem by decoupling data access and business logic from data presentation and user
interaction, by introducing an intermediate component: the controller.



1.3.1 Model

The Model is whe
re we define all of the entities which will be acting and acted upon
within the domain of our application. These entities are referred to as classes or objects.
Within the model, we describe the classes by defining their relationships to other classes,
the
ir attributes, and the methods performed by or to them.

1.3.2 View

View is where we write code specifically for interfacing with users (those entities that
will be acting on application). The view is all about interfacing outside of the application.

In th
e example of AddressBook application, the view will be where we present our users
with their interface to enter new contacts etc. The view contains all of the presentation
logic necessary for our users to interface or initiate actions on our application.

1
.3.3 Controller

The controller interprets the users’ interactions and interfaces with the view and model.
For example, in our AddressBook application, the user may click on a link to list person.
The link was presented by the view to the user. Once he/she
clicks on the link, he/she has
initiated an action. This action will then call the model for the contacts with a sort
method. The controller contains no business logic and deals strictly with exposing
methods contained in the models to the user through the

view.

The controller processes all of the user events or actions and provides the interface
between the view and model.


2. Installing the Software

The web application we will develop is on Windows. But, you can still follow along if
you use a Linux or M
acintosh system, but your screen will look different from the screen
shots shown below and you will have to install software packages built specifically for
your system.

2.1.The direct procedure for the download is

Instant Rails is a one
-
stop Rails runtim
e solution containing Ruby, Rails, Apache, and
MySQL, all preconfigured and ready to run. No installer, you simply drop it into the
directory of your choice and run it. It does not modify your system environment.

Web site is :
-

http://rubyforge.org/projects/instantrails/





2.2 Install and Verify

Follow these steps to install Instant Rails and verify that it is working properly:


* Download and unzip the Instant Rails zip file.


* Make sure

there are no space characters in the installation path, and then start
InstantRails.exe.


* Instant Rails will detect that it is being started from a new directory and ask if you
want to have it update the paths in the all of the configuration files...

Just say yes.


* Click on the I button to drop down the main menu and select


Configure > Windows Hosts file.


* In the editor that pops up, add this line to the end of the file:


127.0.0.1 www.mycookbook.com


* Save the change an
d exit the editor.


* In the main menu, select Rails Applications > Manage Rails Applications...


* Check the checkbox next to the cookbook application.


* Click on the "Start with Mongrel" button.


* Once the Mongrel server says "Mongrel avail
able at 0.0.0.0:3001", open our browser
and go to: http://www.mycookbook.com/

You should now be using a running Rails application! Poke around in the menus to see
what's there.



3. Developing a Web Application

3.1 Step 1 : Creating an Empty Rails Web App
lication


In this step, we will use one such helper script to create the entire directory structure and
the initial set of files to start our address book application.

1.

Open a command window and navigate to where you want to create this
addressbook web appl
ication. We used
c:
\
Instantrails
.

2.

Run the command:

rails addressbook

This will create a
addressbook

subdirectory containing a complete directory tree
of folders and files for an empty Rails application.







3.2

Step 2: Testing the Empty Web Application

Let's start Apache web server and then browse to addressbook application.



In open command window, move into your addressbook directory.



Run the command:

ruby script
\
server

to start the server.










Now open your browser and browse to
http://127.0.0.1:3000/
. You should see the
something like screen

Leave the command window open and the web server running, as we will be using it as
we proceed.






3.3 A Rails Application's Directory Structure


When the
rails

help
er script is used to create your empty application, it created the
entire directory structure for the application.


Most of our development work will be creating and editing files in the
c:
\
Instantrails
\
addressbook
\
app

subdirectories. Here's a quick rundo
wn of how to use
them.



The
controllers’

subdirectory is where Rails looks to find controller classes. A
controller handles a web request from the user.



The
views

subdirectory holds the display templates to fill in with data from our
application, convert t
o HTML, and return to the user's browser.



The
models

subdirectory holds the classes that model and wrap the data stored in
our application's database. In most frameworks, this part of the application can
grow pretty messy, tedious, verbose, and error
-
pron
e.



The
helpers

subdirectory holds any helper classes used to assist the model, view,
and controller classes. This helps to keep the the model, view, and controller code
small, focused, and uncluttered

Controller classes handle web requests from the user.
The URL of the request maps to a
controller class and a method within the class.

Leave your existing command window open with the web server running, open a second
command window, and navigate to the application's base directory,
c:
\
Instantrails
\
addressboo
k
.

3.4 Step 3 :Database

MySQL database has been used and name of the database application is
addressbook_development and can left the password for the development database blank.
That's the convention Rails uses when it generates the configuration file f
or the database.
Othersise if set up had been the development database to require a password, we would
need to tell Rails what password to use t o access it. To change either the name of the
database we want to use or the password we use to access it, edit

..
\
addressbook
\
config
\
database.yml
.

So now we need to create the
addressbook_development

database.

Check for MySQL engine whether it is running or not. In the Command window log in as
the root user.

mysql
-
u root
-
p

Press <Enter> at the password prompt.


And then create database:

create database addressbook_development;

As we are working on Windows, we also need to grant access to the database to a user
named
ODBC
. If we don't, we'll get an error from MySQL when we try to access it in a
Command window. T
o grant access just type in as

grant all on addressbook_development.* to 'ODBC'@'localhost';

exit

and return to the operating system



3.4.1 Create the tables your app will need to produce that feature


drop table if exists addresses;

create table addre
sses (


id int not null auto_increment,


name varchar(40) not null,


street varchar(30) not null,


city


varchar(20) not null,


state


varchar(20) not null,


z
ipcode varchar(15) not null,


email varchar(30) null,


phone1 varchar(12) not null,


phone2 varchar(12) null,


fax varchar(20) null,


website var
char(50) null,


primary key(id)

) engine=InnoDB;


In the Command window, change into the
addressbook

directory and enter:

mysql addressbook_development <db
\
addressbook.sql

MySQL executes the script and returns to the command line without telling u
s anything.
That means everything went OK. Our database now contains one empty table:
addresses
.


3.5 Step 4 : Generate a code base to use the table(s)


Now that we have tables that Rails can look at, we ask it to generate our basic
application. In Rails

lingo, this is
scaffolding
. In the Command window, making sure we
are still in the
addressbook

subdirectory, enter:

ruby script
\
generate scaffold address


This tells Rails to generate the scaffolding: the basic model, controller, and view files for
the
part of our application that will use the addresses table. In the command line we just
entered, we told Rails about two files in particular: the model (the first argument) and the
controller (the second/last argument). Rails named the model file
address.rb

and placed it
in the
..
\
addressbook
\
app
\
models
\

subdirectory. In like fashion, it named the controller
file
addresses_controller.rb

and placed it in the
..
\
addressbook
\
app
\
controllers
\

subdirectory.

Type the url
http://127.0.0.1:3000/addresses/list

in Internet browser for application
addressbook to run.











3.6 Modifying the colors and fonts in stylesheets


Here we are modifying the colors and fonts in stylesheets “scaffold.css” that is located

at
C:
\
InstantRails
\
addressbook
\
public
\
stylesheets
\


scaffold.css

body { background
-
color: #eee; color: #333; }



body, p, ol, ul {


font
-
family: verdana, arial, helvetica, sans
-
serif;


font
-
size: 18px;

}


td{

font
-
family: verdana, arial, helvetica, sa
ns
-
serif;


font
-
size: 15px;


}


pre {


background
-
color: #eee;


padding: 10px;


font
-
size: 11px;

}


a { color: #000; }

a:visited { color: #666; }

a:hover { color: #fff; background
-
color:#000; }


.fieldWithErrors {


padding: 2px;


background
-
color:

red;


display: table;

}


#errorExplanation {


width: 400px;


border: 2px solid red;


padding: 7px;


padding
-
bottom: 12px;


margin
-
bottom: 20px;


background
-
color: #f0f0f0;

}


#errorExplanation h2 {


text
-
align: left;


font
-
weight: bold;


padding
: 5px 5px 5px 15px;


font
-
size: 12px;


margin:
-
7px;


background
-
color: #c00;


color: #fff;

}


#errorExplanation p {


color: #333;


margin
-
bottom: 0;


padding: 5px;

}


#errorExplanation ul li {


font
-
size: 12px;


list
-
style: square;

}


div.uploadS
tatus {


margin: 5px;

}


div.progressBar {


margin: 5px;

}


div.progressBar div.border {


background
-
color: #fff;


border: 1px solid grey;


width: 100%;

}


div.progressBar div.background {


background
-
color: #333;


height: 18px;


width: 0%;

}








4. References



http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting
-
ruby
-
on
-
rails
-
revisited.html?page=2



http://www.onlamp.com/pub/a/onlamp/2007/01/05/revisiting
-
ruby
-
on
-
rails
-
revisited
-
2.html?page=4



h
ttp://www.onlamp.com/pub/a/onlamp/2005/10/13/what_is_rails.html?page=7



http://www.onlamp.com/pub/a/onlamp/2005/03/03/rails.html?page=2



http://www.digitalmediaminute.com/article/1816/top
-
ruby
-
on
-
rails
-
tutorials



http://www
-
128.ibm.com/developerworks/linux/librar
y/l
-
rubyrails/



Model view architecture :
-



http://en.wikipedia.org/wiki/Model
-
view
-
controller



http://java.sun.com/blueprints/
patterns/MVC.html



http://www.themolehill.com/index.php/2006/07/29/beginners
-
guide
-
model
-
view
-
controller
-
mvc
-
architecture/
















5 APPENDIX

5.1
APPENDIX

I
: Flow of Application












5.2 APPENDIX II: Code


addresses_controller.rb

class AddressesController < ApplicationController


def index


list


render :action => 'list'


end



# GETs should be safe (see
http://www
.w3.org/2001/tag/doc/whenToUseGet.html)


verify :method => :post, :only => [ :destroy, :create, :update ],


:redirect_to => { :action => :list }



def list


@address_pages, @addresses = paginate :addresses, :per_page => 10


end



def show



@address = Address.find(params[:id])


end



def new


@address = Address.new


end




def create


@address = Address.new(params[:address])


if @address.save


flash[:notice] = 'Address was successfully created.'


redirect_to :action =
> 'list'


else


render :action => 'new'


end


end



def edit


@address = Address.find(params[:id])


end



def update


@address = Address.find(params[:id])


if @address.update_attributes(params[:address])


flash[:notice] = 'Addr
ess was successfully updated.'


redirect_to :action => 'show', :id => @address


else


render :action => 'edit'


end


end



def destroy


Address.find(params[:id]).destroy


redirect_to :action => 'list'


end

end






edit .rhtml

<h2>
<center> <u>Address Book</u></center></h2>

<center>

<% form_tag :action => 'update', :id => @address do %>


<%= render :partial => 'form' %>


<%= submit_tag 'Edit' %>

<% end %>

<%= link_to 'Show', :action => 'show', :id => @address %> |

<%= link_to 'Bac
k', :action => 'list' %>

</center>


list.rhtml


<h2><center>List of Addressbook</center></h2>


<table border =2>


<tr>


<% for column in Address.content_columns %>


<th><%= column.human_name %></th>


<% end %>


</tr>



<% for address in @addresse
s %>


<tr>


<% for column in Address.content_columns %>


<td><%=h address.send(column.name) %></td>


<% end %>


<td><%= link_to 'Show', :action => 'show', :id => address %></td>


<td><%= link_to 'Edit', :action => 'edit', :id => address %></td>


<td><%= link_to 'Destroy', { :action => 'destroy', :id => address }, :confirm =>
'Are you sure?', :method => :post %></td>


</tr>

<% end %>

</table>


<%= link_to 'Previous page', { :page => @address_pages.current.previous } if
@address_pages.current.
previous %>

<%= link_to 'Next page', { :page => @address_pages.current.next } if
@address_pages.current.next %>


<br />


<%= link_to 'Create New ', :action => 'new' %>


new.rhtml

<h2> <center>Address Book<center></h2>


<center>

<% form_tag :action => 'cre
ate' do %>


<%= render :partial => 'form' %>


<%= submit_tag "Create" %>

<% end %>


<%= link_to 'Back', :action => 'list' %>

<center>





show.rhtml


<h2><center><u>Address Book</u></center></h2>

<table border=1 align="center" width="500">

<% for column
in Address.content_columns %>

<tr>

<td>


<b><%= column.human_name %>:</b>

</td>

<td>

<%=h @address.send(column.name) %>

</td>

</tr>

<% end %>

</table>

<br>

<center>

<%= link_to 'Edit', :action => 'edit', :id => @address %> |

<%= link_to 'Back', :action =
> 'list' %>

</center>


-
form.rhtml

<%= error_messages_for 'address' %>


<!
--
[form:address]
--
>

<table border =2 align ="center" width = "500px" >

<tr>

<td width = "140px">

<p align ="left"><label for="address_name">Name</label></td>

<td><%= text_field 'addr
ess', 'name' %></p></td>

</tr>


<tr>

<td width = "140px">

<p align ="left"><label for="address_street">Street</label></td>

<td><%= text_field 'address', 'street' %></p></td>

</tr>


<tr>

<td width = "140px">

<p align ="left"><label for="address_city">City
</label></td>

<td><%= text_field 'address', 'city' %></p></td>

</tr>


<tr>

<td width = "140px">

<p align ="left"><label for="address_state">State</label></td>

<td><%= text_field 'address', 'state' %></p></td>

</tr>


<tr>

<td width = "140px">

<p align ="l
eft"><label for="address_zipcode">Zipcode</label></td>

<td><%= text_field 'address', 'zipcode' %></p></td>

</tr>


<tr>

<td width = "140px">

<p align ="left"><label for="address_email">Email</label><br/></td>

<td><%= text_field 'address', 'email' %></p></
td>

</tr>


<tr>

<td width = "140px">

<p align ="left"><label for="address_phone1">Phone1</label></td>

<td><%= text_field 'address', 'phone1' %></p></td>

</tr>


<tr>

<td width = "140px">

<p align ="left"><label for="address_phone2">Phone2</label></td>

<td>
<%= text_field 'address', 'phone2' %></p></td>

</tr>


<tr>

<td width = "140px">

<p align ="left"><label for="address_fax">Fax</label></td>

<td><%= text_field 'address', 'fax' %></p></td>

</tr>


<tr>

<td width = "140px">

<p align ="left"><label for="addre
ss_website">Website</label></td>

<td><%= text_field 'address', 'website' %></p></td>

</tr>

</table>


<!
--
[eoform:address]
--
>





5.3
APP
ENDIX III:
ScreenShots of AddressBook Application


Screen Shot of List


Screen Shot of Create


Screen Shot of
Edit



Screen Shot of Show