Professional Documents
Culture Documents
InstantRails Working
InstantRails Working
Examples
appln1
app
helpers
controllers
views models
application hello_controller
hello
layouts
hellopage.rhtml
<html>
<body>
</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.
<html>
<body>
</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>
</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
end
result
@n1_value=params[:n1]
@n2_value=params[:n2]
@sum=@n1_value+@n2_value
@avg=@sum/2
end
end
firstpg.rhtml
</form>
store.rhtml
search.rhtml
</form>
result.rhtml
@daccno = i.accno
@dtitle = i.title
@dauthor = i.author
@dedition = i.edition
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
</table>
❖ 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
Note: * Database name must be same as application name with extension as _development.
e.g.: “book” is name of model, controller and “books” is name of the table