0% found this document useful (0 votes)
57 views34 pages

Using Rails Shortcuts For HTML Controls: Text - Field - Tag

1. The document describes how to use various Rails form controls and shortcuts like text_field_tag, check_box_tag, select_tag to build forms. 2. It also explains how to tie form fields to models using attributes, and store data in sessions across requests. 3. The last part shows how to perform database queries in Rails using ActiveRecord and display the results in a view.

Uploaded by

sundhar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views34 pages

Using Rails Shortcuts For HTML Controls: Text - Field - Tag

1. The document describes how to use various Rails form controls and shortcuts like text_field_tag, check_box_tag, select_tag to build forms. 2. It also explains how to tie form fields to models using attributes, and store data in sessions across requests. 3. The last part shows how to perform database queries in Rails using ActiveRecord and display the results in a view.

Uploaded by

sundhar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Using Rails Shortcuts for HTML

Controls
• To create a text field using the Rails
text_field_tag, follow these steps..
• 1. Create a new application named textfields2:
• C:\rubydev>Rails textfields2

• 2. Change directories to the textfields2 directory:


• C:\rubydev>cd textfields2
• C:\rubydev\textfields2>
• 3. Add a controller named look:
C:\rubydev\textfields2>ruby script/generate
controller Look
• 4. open look_controller.rb, adding this code:
class LookController < ApplicationController
def at
@data = params[:text1]
end
def input
end
end
• 5. Create a new view template for the application,
\views\look\input.rhtml:
<body>
<h1>Working With Text Fields</h1>
<br>
<%= start_form_tag ({:action => “at”}, {:method =>
“post”}) %>
Please enter your name.
<%= text_field_tag (“text1”, “”, {“size” => 30}) %><br>
<input type=”submit”/>
<%= end_form_tag %>
</body>
6. Create another new view template for the
application, app\views\ look\at.rhtml:
<body>
<h1>Using HTML Control Shortcuts</h1>
This application uses Rails HTML control shortcuts.
<br>
<br>
Your name is <%= @data %>.
<br>
<br>
</body>
• 7. Start the WEBrick server:
C:\rubydev\textfield2>ruby script/server
• You pass the start_form_tag method a
specifying the action, and an optional hash
specifying options—the two possible options
are :method, which can be “get” or “post” and
:multipart, which can be “true” or “false”.
• There’s also a check_box_tag method to
create checkboxes, a radio_button_tag
method to create radio buttons, and so on.
How to use Check box in rails?
<%= start_form_tag ({:action => “at”}, {:method
=> “post”}) %>
Would you like a raise?
<br>
<br>
<%= check_box_tag (“check1”, “Yes”, false)%>Yes
<br>
<br>
<input type=”submit”/>
<%= end_form_tag %>
And you set up the controller named look with
two actions, input and at:
class LookController < ApplicationController
def at
@data = params[:check1]
end
def input
end
end
 Finally views\look\at.rhtml, looks like..
<body>
<h1>Reading data from checkboxes</h1>
This Ruby on Rails application reads data from
checkboxes.
<% if @data %>
You clicked yes.
<% else %>
You did not click yes.
<% end %>
</body>
Using select Control
<body>
<h1>Working With Select Controls</h1>
This Ruby on Rails application lets you read data
from select controls.
<br>
<%= start_form_tag ({:action => “at”}, {:method
=> “post”}) %>
Select your new car’s color.
<br>
<br>
<%= select_tag (“select1[]”, “<option
value=’red’>red<option
value=’green’>green<option value=’blue’>blue”,
{:multiple => true}) %>
<br>
<br>
<input type=”submit”/>
<%= end_form_tag %>
</body>
Working with Models
• To use a model, follow these steps:
1. Create a new application named modeler:
C:\rubydev>Rails modeler
2. Change directories to the modeler directory:
C:\rubydev>cd modeler
C:\rubydevmodeler>
• 3. Add a controller named look:
• C:\rubydev\modeler>ruby script/generate
controller Look
4. Create a new file, app\models\cruncher.rb,
placing this code in that file:
class Cruncher
def crunch
return 5
end
end
5. Edit app\controllers\look_controller.rb,
adding this code:
class LookController < ApplicationController
def at
@cruncher = Cruncher.new
@data = @cruncher.crunch
end
end
6. Create a new view template for the
application, app\views\look\at.rhtml:
<html>
<body>
<h1>Working With Models</h1><br>
This application fetches data from a model.<br>
The fetched data is: <%= @data %>.
</body>
</html>
7. Start the WEBrick server:
C:\rubydev\modeler>ruby script/server
• creating an object of the Cruncher class and
calling the crunch method to recover the data
from the model:
class LookController < ApplicationController
def at
@cruncher = Cruncher.new
@data = @cruncher.crunch
end
end
Tie a Text Field to a Model
• 1. Create a new application named textfields3:
C:\rubydev\ch05>Rails textfields3
• 2. Change directories to the textfields3
directory:
C:\rubydev>cd textfields3
C:\rubydev\textfields3>
• 3. Add a controller named look:
C:\rubydev\textfields3>ruby script/generate
controller Look
• 4. Edit \app\controllers\look_cotroller.rb,
adding this code:
class LookController < ApplicationController
def at
@data_hash = params[:cruncher]
@cruncher = Cruncher.new(@data_hash[:crunch])
@data = @cruncher.crunch
end
def input
end
end
• 5. Create a new file, \app\models\cruncher.rb,
add this code…
class Cruncher
attr_reader :crunch
attr_writer :crunch
def initialize(data)
@crunch = data
end
end
6. Create a new view template for the
application,\app\views\look\ input.rhtml:
<body>
<h1>Working With Text Fields</h1>
This Ruby on Rails application lets you read data from
text fields.
<%= start_form_tag ({:action => “at”}, {:method =>
“post”}) %>
Please enter your name.
<%= text_field (“cruncher”, “crunch”, {“size” => 30}) %>
<input type=”submit”/>
<%= end_form_tag %>
</body>
</html>
7. Create another new view template for the
application, \app\views\look\at.rhtml:
<body>
<h1>Using HTML Control Shortcuts</h1>
This application uses Rails HTML control shortcuts.
<br>
<br>
Your name is <%= @data %>.
<br>
<br>
</body>
• 8. Start the WEBrick server:
• C:\rubydev\textfield3>ruby script/server
Storing Data in Sessions
 A session is made up of memory on the server,
and you access that memory using the hash
named session.
 To store a data item in the session, you just
place it in the session hash using a key:
• session[:data] = @data
• Then, when you want to recover that data
when the same—or another—action is called
• The data in a session isn’t permanent. By
default it times out after 15-20 minutes of
inactivity on the user’s part.
• To store data in a session, follow these steps

1.Create a new application named sessions:


C:\rubydev>Rails sessions

2. Change directories to the sessions directory:


C:\rubydev>cd sessions
C:\rubydev\sessions>
3. Add a controller named look:
C:\rubydev\sessions>ruby script/generate
controller Look
4. open look_controller.rb, and add following this
code
class LookController < ApplicationController
def at
@counter1 = 1
if(session[:counter2])
@counter2 = session[:counter2]
@counter2 += 1
session[:counter2] = @counter2
else
@counter2 = 1
session[:counter2] = @counter2
end
end
end
5. Create a new view template for the
application, app\views\look\
input.rhtml:
<html>
<head>
<title>Using Sessions</title>
</head>
<body>
<h1>Working With Sessions</h1>
This Ruby on Rails application lets you store data
in sessions.
<%= start_form_tag ({:action => “at”}, {:method
=> “post”}) %>
<br>
Counter 1: <%= @counter1 %>.
<br>
Counter 2: <%= @counter2 %>.
<br>
<br>
<input type=”submit”/>
<%= end_form_tag %>
</body>
</html>
• 6. Start the WEBrick server:
C:\rubydev\sessions>ruby script/server
• 7. Navigate to
http://localhost:3000/look/input. The
application displays the values of two
• counters in the web page . Counter 1 is not
stored in a session, But Counter 2 is stored in
the session, Counter 1 is reinitialized each
time you call the action
How to use Mysql queries in ROR
• 1)ruby script/generate controller Buy index
• 2) You can create a class method in the model,
\app\models\products.rb, named, say,
return_products, like this:
class product < ActiveRecord::Base
def self.return_products
find(:all,:order => “name description”,:conditions =>
“price <= 20.00”)
end
end
There are actually three ways to call find:
• ❑ find(:id)—Finds a record by ID
• ❑ find(:first)—Finds the first record
• ❑ find(:all)—Returns all the records in the
table
Those other requirements are SQL-oriented; if
you know SQL, here they are:
• Ruby SQL Information
• :conditions SQL code indicating a condition or
conditions to match.
• :group Specifies an attribute indicating how the
result should be grouped, making use of the
SQL GROUP BY clause.
• :include Specifies associations to be included
using SQL LEFT OUTER JOINs.
• :joins Specifies additional SQL joins.
• :limit Specifies an integer setting the upper limit
of the number of rows to be returned.
• :offset Specifies an integer indicating the offset
from where the rows should be returned.
• :order Lets you specify the fields to set the order
of returned records.
• :readonly Marks the returned records read-only.
• :select A SQL SELECT statement, as in SELECT *
FROM items.
• To start the process of displaying the items for
sale when the buy controller’s index action is
called, you call return_items in the index
action:
class BuyController < ApplicationController
def index
@items = Item.return_items
end
end
Show Database Items in a Web Page
<html>
<head>
<title>The Store</title>
</head>
<body>
<h1>Buy From Our Store!</h1>
<b>Welcome to the store.</b>
<br>
<b><i>Please buy a lot of items, hankyou.</i></b>
<br>
<br>
<table cellpadding=”6”>
<% for item in @items %>
<tr>
<td><b><%=h item.name %></b></td>
<td><%=h item.description %></td>
</tr><% end %>
</table>
</body>
</html>

You might also like