Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

InstantRails Working

Example 1. Static web page (Hello program)


Open instant application window:

➢ click “I” on the left border of window


➢ click RailsApplication
➢ Open Ruby Console Window
➢ Open command window in the following directory
D:\myrails\InstantRails-1.7-win\InstantRails\rails-apps
➢ Create new directory for all applications
mkdir examples
cd examples
➢ Create new rails application with name appln1 using rails command
rails appln1
cd appln1
➢ Observe the directories created [model, views, controller] in app directory
➢ Generate controller class with the name hello
ruby script/generate controller hello
This controller class is specific to this application i. e. appln1, and controls this
application
The controller class contains action methods for all rhtml files of application
All the rhtml files must be stored in examples\appln1\app\views\hello directory
➢ Create hellopage.rhtml and save in examples\appln1\app\views\hello
➢ Add the action method for this hello.rhtml in HelloController(Hello_Controller)
class as below:
[ open Hello_Controller file by right clicking and open with notepad
Add a method as below:
Class HelloController < ApplicationController
def hellopage
end
end

Note: action method don’t contain anything as it is a static document, in case


of dynamic document it can contain variables or do calculations etc.

➢ Start the server with following command


ruby script/server
➢ Run the program on the browser as below:
http://localhost:3000/hello/hellopage

Examples

appln1

app

helpers
controllers
views models

application hello_controller
hello
layouts

hellopage.rhtml

Figure. Directory structure for the rails application


Example 2. Dynamic web page (displays the current date and time)
Open instant application window:

➢ click “I” on the left border of window


➢ click RailsApplication
➢ Open Ruby Console Window
➢ Open command window in the following directory
D:\myrails\InstantRails-1.7-win\InstantRails\rails-apps
➢ Enter into your directory
cd examples
➢ Create new rails application with name appln1 using rails command
rails appln2
cd appln2
➢ Observe the directories created [model, views, controller] in app directory
➢ Generate controller class with the name timer
ruby script/generate controller timer
This controller class is specific to this application i. e. appln2, and controls this
application
The controller class contains action methods for all rhtml files of application
All the rhtml files must be stored in examples\appln2\app\views\timer
directory
➢ Create timepage.rhtml and save in examples\appln2\app\views\timer

<html>

<body>

<p> It is now <%= @t %><br/>

</p>

</body>

</html>

In the above program, Ruby code is embedded in html which is printing the result
produced by respective action method in the Timer Controller. @t is the instance
variable accessing that value, and an equal sign (=) prints on the html page.
➢ Add the action method for this timepage.rhtml in
TimerController(Timer_Controller) class as below:
[ open Timer_Controller file by right clicking and open with notepad
Add a method as below:
Class TimerController < ApplicationController
def timepage
@t=Time.now
end
end

Action method is using a Ruby built-in function Time.now to get current time and
date.

➢ Start the server with following command


ruby script/server
➢ Run the program on the browser as below:
http://localhost:3000/timer/timepage

Example 3. Form processing (finding average of two numbers and display)

❖ Setting up the Application


1. Create new rails application with name appln3 using rails command
a. rails appln3
b. cd appln3
2. Generate controller class with the name home
a. ruby script/generate controller home
❖ Add rhtml files in ‘home’ subdirectory of ‘views’ directory

1. Create form_page.rhtml and save in examples\appln3\app\views\home

<html>

<body>

<form action=”result” method=”post”>

<label> Num1: </label> <input type=”text” name=”n1”/>

<label> Num2: </label> <input type=”text” name=”n2”/>

<input type=”submit” name=”AVERAGE”/>


</form>

</body>

</html>

In the above program, value of the form action attribute is ‘result’. It is the page
(result.rhtml) to be opened after submitting the form. The value of method
attribute is ‘post’. Rails require that POST must be used always.
2. Create result.rhtml and save in examples\appln3\app\views\home

<html>

<body>

<p> The average of two numbers entered : <br/>

Average: <%= @avg%>

</p>

</body>

</html>
In the above program, Ruby code is embedded in html which is printing the result
produced by respective action method in the Home Controller. @avg is an instance
variable, which holds the result after calculations in action method.

❖ The Controller
Add the action methods for above rhtml programs in the HomeController

Class HomeController < ApplicationController


def front_page

end
result
@n1_value=params[:n1]
@n2_value=params[:n2]
@sum=@n1_value+@n2_value
@avg=@sum/2
end
end

❖ Start the server with following command


ruby script/server

❖ Run the program on the browser as below:


http://localhost:3000/home/form_page

Example 4. Database application (BOOK information)

❖ Setting up the Application


1. Create new rails application with name appln4 using rails command
a. rails appln4
b. cd appln4
2. Generate controller class with the name book

ruby script/generate controller book

3. Generate model class with the name book

ruby script/generate model book

❖ Add rhtml files in ‘book’ subdirectory of ‘views’ directory

1. Create firstpg.rhtml, store.rhtml, search.rhtml and result.rhtml files and save


in examples\appln4\app\views\book

firstpg.rhtml

<h1> My Library Rails Application </h1>

<form action="store" method="post" >

Accno : <input type="text" name="accno" /> <br/>

Title : <input type="text" name="title" /> <br/>


Author : <input type="text" name="author" /> <br/>

Edition : <input type="text" name="edition" /> <br/>

Publisher: <input type="text" name="publisher" /> <br/>

<input type="submit" value="submit" /><br/>

</form>

store.rhtml

<h2> Results after submiting data </h2>

<%= @msg %>

search.rhtml

<h1> Search for a book Rails Application </h1>

<form action="result" method="post" >

Title : <input type="text" name="title" />

<input type="submit" value="submit" />

</form>

result.rhtml

<h1> result page </h1>

<% @bookary.each do |i|

@daccno = i.accno

@dtitle = i.title

@dauthor = i.author

@dedition = i.edition

@dpublisher = i.publisher %>


<table border=4>

<tr>

<th> Accno </th> <td> <%= @daccno %></td>

</tr>

<tr>

<th> Title </th> <td> <%= @dtitle %></td>

</tr>

<tr>

<th> Author</th> <td> <%= @dauthor %></td>

</tr>

<tr>

<th> Edition </th> <td> <%= @dedition %> </td>

</tr>

<tr>

<th> Publisher </th><td> <%= @dpublisher %> </td>

</tr>

</table>

<% end %>

<h2> end of result </h2>

❖ The Controller
Add the action methods for above rhtml programs in the BookController
class BookController < ApplicationController

def firstpg

end
def store
book = Book.new
book.accno = params[:accno]
book.title = params[:title]
book.author = params[:author]
book.edition = params[:edition]
book.publisher = params[:publisher]
if book.save
@msg = 'saved'
else
@msg = 'Unable to save '
end
end

def search

end

def result
@title = params[:title]
@bookary = Book.find(:all, :conditions => ["title = ? ", @title])
end

end

❖ Create the database

On ruby console window

> mysql -u root

mysql > create database appln4_development;


mysql > use appln4_development;
mysql > create table books(accno char(20), title char(20), author char(20), edition
char(20), publisher char(20));
mysql > show tables;
mysql > select * from books;
❖ Start the server with following command
ruby script/server

❖ Run the program on the browser as below:


http://localhost:3000/book/firstpg

Note: * Database name must be same as application name with extension as _development.

e.g.: appln4_development is database name

* Model name, controller name ad table name all are same

But table name ends with s.

e.g.: “book” is name of model, controller and “books” is name of the table

You might also like