Prepared by Prof. Rama Satish KV, RNSIT, Bangalore.
Creating Books Database
mysql - u r oot
cr eat e dat abase l ab12_devel opment ;
cr eat e dat abase l ab12_t est ;
cr eat e dat abase l ab12_pr oduct i on;
use l ab12_devel opment ;
cr eat e t abl e books (
i d i nt not nul l aut o_i ncr ement ,
name var char ( 80) not nul l ,
descr i pt i on t ext not nul l ,
pr i ce deci mal ( 8, 2) not nul l ,
pr i mar y key( i d)
) ;
Creating lab12 Project
r ai l s - d mysql l ab12
Creating controller, model and view from database
r uby scr i pt / gener at e scaf f ol d Book name: st r i ng descr i pt i on: t ext pr i ce: f l oat
Starting Rails Server
r uby scr i pt / ser ver
Executing in the web browser
ht t p: / / l ocal host : 3000/ books
Creating main (new) controller for searching book
r uby scr i pt / gener at e cont r ol l er mai n
Opening main controller program
edi t app\ cont r ol l er s\ mai n_cont r ol l er . r b
Create two views welcome and result
cl ass Mai nCont r ol l er < Appl i cat i onCont r ol l er
end
def welcome
@num_books = Book. count
end
def result
@booki d = par ams[ : si d]
@bookz = Book. f i nd( : al l , : condi t i ons => " i d = #{@booki d}" )
end
Prepared by Prof. Rama Satish KV, RNSIT, Bangalore.
Create Result view file
> not epad app\ vi ews\ mai n\ r esul t . r ht ml
<ht ml >
<t i t l e> Wel come t empl at e f or books </ t i t l e>
<body>
<p> Ent er ed book i d i s <%= @booki d %> </ p>
<t abl e bor der =1>
<t r ><t h>Book I d</ t h><t h>Book Name</ t h><t h>Det ai l s </ t h> <t h>Pr i ce </ t h> </ t r >
<%@bookz. each do | bk|
@i d = bk. i d
@name = bk. name
@descp = bk. descr i pt i on
@pr i ce = bk. pr i ce %>
<t r >
<t d> <%= @i d %></ t d>
<t d><%= @name %> </ t d>
<t d><%= @descp %></ t d>
<t d> <%= @pr i ce %></ t d>
</ t r >
<%end %>
</ t abl e>
</ f or m>
</ body>
</ ht ml >
Create Welcome view file
> not epad app\ vi ews\ mai n\ wel come. r ht ml
<ht ml >
<t i t l e> Wel come t empl at e f or books </ t i t l e>
<body>
<p> Tot al number of books = <%= @num_books %> </ p>
<f or mact i on = " r esul t " >
Ent er Sear chi ng El ement : <i nput t ype=" t ext " name=" si d" / >
<i nput t ype=submi t val ue=" Sear ch" / >
</ f or m>
</ body>
</ ht ml >
Starting Rails Server
r uby scr i pt / ser ver
Executing in the web browser
ht t p: / / l ocal host : 3000/ mai n/ wel come
Prepared by Prof. Rama Satish KV, RNSIT, Bangalore.
[Link]
[Link]