You are on page 1of 10

Web programming lab

Prepared by : Badrinath K

12. Build a Rails application to accept book information viz. Accession number, title,
authors, edition and publisher from a web page and store the information in a database
and to search for a book with the title specified by the user and to display the search results
with proper headings.
In windows XP only :
Save InstantRails-2.0-win to C drive and extract it.
Create shortcut on desktop for InstantRails-2.0-win logo

Starting Instant Rails


To start Instant Rails, double-click the Instant Rails icon that you put on the desktop. When you
first start Instant Rails, it will ask you to confirm that it is OK to regenerate the configuration
files. Click on OK. It will now attempt to start both the Apache web server and the MySQL
server. You may get a query from your firewall software asking you to confirm you want these to
be accessible.
It should now show a new window. Two traffic-lights at the top of the window should indicate
that it has started the Apache web server and the MySQL server.

Dept of ISE, SJCIT

Page 1

Web programming lab

Prepared by : Badrinath K

If you click on the Apache/MySQL buttons, you will get a menu enabling you to start/restart
these servers.
To the left of the Apache button, there is another button that has the Instant Rails logo. Clicking
this button reveals a drop-down menu with the following options:
Help
Log Files
Configure
Rails Applications
Restart Servers
Stop Servers and Exit

Dept of ISE, SJCIT

Page 2

Web programming lab

Prepared by : Badrinath K

Following will open :

Dept of ISE, SJCIT

Page 3

Web programming lab

Prepared by : Badrinath K

Start typing the following in rails prompt :


rails d mysql lab12
cd lab12
mysql u root
create database lab12_development;
create database lab12_test;
create database lab12_production;
use lab12_development;
create table books (id int not null auto_increment, accession_num int not null, title vachar(80)
not null, author varchar(80) not null, edition int not null, publisher varchar(80) not null,
primary key(id));
mysql> exit
ruby script/generate scaffold Book accession_num:int title:string author:string edition:int
publisher:string;
START SERVER :
ruby script/server
URL
http://localhost:3000/books

You can now access your web application by using a browser to visit:
http://localhost:3000/

Dept of ISE, SJCIT

Page 4

Web programming lab

Prepared by : Badrinath K

Type table name: localhost:3000/books

Click New book Link:


Dept of ISE, SJCIT

Page 5

Web programming lab

Dept of ISE, SJCIT

Prepared by : Badrinath K

Page 6

Web programming lab

Dept of ISE, SJCIT

Prepared by : Badrinath K

Page 7

Web programming lab

Prepared by : Badrinath K

In rails prompt press ctrl+c server will stop


Type following command in rails prompt
ruby script/generate controller main
Use the following path to have the program file
C:\Rails\rails_apps\lab12\app\controllers\main_controller.rb
class MainController < ApplicationController
def welcome
@num_books=Book.count
end
def result
@title=params[:title]
@bookz=Book.find(:all, :conditions=>"title=#{@title}")
end
end
Use the following path to save the program file
C:\Rails\rails_apps\lab12\app\views\main
result.rhtml
<html>
<title>welcome template for books</title>
<body>
<p> entered book title is:<%=@title%></p>
<table border=1>
<tr>
<th>Accession number</th>
<th>Title</th>
<th>Author</th>
<th>Edition</th>
<th>Publisher</th>
</tr>
<% @bookz.each do |bk|
@accession_num=bk.accession_num
@title=bk.title
@author=bk.author
@edition=bk.edition
@publisher=bk.publisher %>
<tr>
<td><%=@accession_num%></td>
<td><%=@title%></td>
Dept of ISE, SJCIT

Page 8

Web programming lab

Prepared by : Badrinath K

<td><%=@author%></td>
<td><%=@edition%></td>
<td><%=@publisher%></td>
</tr>
<%end%>
</table>
</form>
</body>
</html>

Welcome.rhtml
<html>
<title>welcome template for books</title>
<body>
<p>total number of books=<%=@num_books %></p>
<form action="result">
enter searching book tittle : <input type="text" name="title"/>
<input type=submit value="search"/>
</form>
</body>
</html>
Save as type : all files
Encoding : utf-8

Dept of ISE, SJCIT

Page 9

Web programming lab

Dept of ISE, SJCIT

Prepared by : Badrinath K

Page 10

You might also like