Download - Rails Ppt2
-
8/8/2019 Rails Ppt2
1/34
-
8/8/2019 Rails Ppt2
2/34
-
8/8/2019 Rails Ppt2
3/34
-
8/8/2019 Rails Ppt2
4/34
6 . Create another new view template for theapplication, app\views\ look\at . rhtml:
Using HTML Control ShortcutsThis application uses Rails HTML control shortcuts .
Your name is .
-
8/8/2019 Rails Ppt2
5/34
7 . Start the WEBrick server:
C:\rubydev\textfield2>ruby script/serverYou pass the start_form_tag method aspecifying the action, and an optional hashspecifying 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_tagmethod to create radio buttons, and so on .
-
8/8/2019 Rails Ppt2
6/34
How to use Check box in rails? at }, {:method
=> post }) %>Would you like a raise?
Yes
-
8/8/2019 Rails Ppt2
7/34
A nd you set up the controller named look withtwo actions, input and at :
class LookController < A pplicationControllerdef at
@data = params[:check1]enddef input
endend
-
8/8/2019 Rails Ppt2
8/34
Finally views\look\at .rhtml, looks like ..
Reading data from checkboxesThis Ruby on Rails application reads data from
checkboxes .
You clicked yes .
You did not click yes .
-
8/8/2019 Rails Ppt2
9/34
Using select ControlWorking With Select ControlsThis Ruby on Rails application lets you read data
from select controls .
at }, {:method=> post }) %>Select your new car s color .
-
8/8/2019 Rails Ppt2
10/34
true}) %>
-
8/8/2019 Rails Ppt2
11/34
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 modelerC:\rubydevmodeler>
3 . A dd a controller named look:C:\rubydev\modeler> ruby script/generatecontroller Look
-
8/8/2019 Rails Ppt2
12/34
4 . Create a new file, app\models\cruncher . rb,placing this code in that file:
class Cruncherdef crunchreturn 5
endend
-
8/8/2019 Rails Ppt2
13/34
5. Edit app\controllers\look_controller . rb,adding this code:
class LookController < A pplicationControllerdef at@cruncher = Cruncher .new@data = @cruncher .crunchend
end
-
8/8/2019 Rails Ppt2
14/34
6 . Create a new view template for theapplication, app\views\look\at .rhtml:
Working With Models
This application fetches data from a model .
The fetched data is: .7 . Start the WEBrick server:
C:\rubydev\modeler>ruby script/server
-
8/8/2019 Rails Ppt2
15/34
creating an object of the Cruncher class andcalling the crunch method to recover the datafrom the model:
class LookController < A pplicationControllerdef at@cruncher = Cruncher .new@data = @cruncher .crunchendend
-
8/8/2019 Rails Ppt2
16/34
Tie a Text Field to a Model
1.
Create a new application named textfields3:C:\rubydev\ch0 5 >Rails textfields3
2 . Change directories to the textfields3
directory:C:\rubydev> cd textfields3C:\rubydev\textfields3>
3 . A dd a controller named look:C:\rubydev\textfields3> ruby script/generate
controller Look
-
8/8/2019 Rails Ppt2
17/34
4 . Edit \app\controllers\look_cotroller . rb, addingthis code:
class LookController < A pplicationControllerdef at@data_hash = params[:cruncher]
@cruncher = Cruncher .new(@data_hash[:crunch])@data = @cruncher .crunchend
def inputendend
-
8/8/2019 Rails Ppt2
18/34
5. Create a new file, \app\models\cruncher . rb,add this code
class Cruncherattr_reader :crunchattr_writer :crunchdef initialize(data)@crunch = dataendend
-
8/8/2019 Rails Ppt2
19/34
6 . Create a new view template for theapplication,\app\views\look\ input . rhtml:
Working With Text FieldsThis Ruby on Rails application lets you read data from text
fields .
at }, {:method =>post }) %>Please enter your name .
30}) %>
-
8/8/2019 Rails Ppt2
20/34
7 . Create another new view template for theapplication, \app\views\look\at . rhtml :
Using HTML Control ShortcutsThis application uses Rails HTML control shortcuts .
Your name is .8 . Start the WEBrick server:
C:\rubydev\textfield3> ruby script/server
-
8/8/2019 Rails Ppt2
21/34
Storing Data in Sessions
A session is made up of memory on the server,and you access that memory using the hashnamed session.To store a data item in the session, you justplace it in the session hash using a key:session [:data] = @dataThen, when you want to recover that datawhen the same or another action is called
-
8/8/2019 Rails Ppt2
22/34
The data in a session isn t permanent. Bydefault 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 sessionsC:\rubydev\sessions>
-
8/8/2019 Rails Ppt2
23/34
3 . A dd a controller named look :C:\rubydev\sessions> ruby script/generate
controller Look4 . open look_controller . rb, and add following this
code
class LookController post }) %>
Counter 1: .Counter 2: .
-
8/8/2019 Rails Ppt2
27/34
6 . Start the WEBrick server:
C:\rubydev\sessions>ruby script/server7 . Navigate tohttp://localhost:3000/look/input . Theapplication displays the values of two
counters in the web page . Counter 1 is notstored in a session, But Counter 2 is stored inthe session, Counter 1 is reinitialized each
time you call the action
-
8/8/2019 Rails Ppt2
28/34
How to use Mysql queries in ROR
1)ruby script/generate controller Buy index2) You can create a class method in the model,\app\models\products . rb, named, say,
return_products, like this:class product < A ctiveRecord::Basedef self . return_products
find(:all,:order => name description ,:conditions =>price
-
8/8/2019 Rails Ppt2
29/34
There are actually three ways to call find:
find(:id) Finds a record by IDfind(:first) Finds the first recordfind(:all) Returns all the records in the
table
-
8/8/2019 Rails Ppt2
30/34
Those other requirements are SQL-oriented; if you know SQL, here they are:
R uby SQL Information:conditions SQL code indicating a condition orconditions to match .
:group Specifies an attribute indicating how theresult should be grouped, making use of theSQL GROUP BY clause.
:include Specifies associations to be includedusing SQL LEFT OUTER JOINs.
:joins Specifies additional SQL joins .
-
8/8/2019 Rails Ppt2
31/34
:limit Specifies an integer setting the upper limitof the number of rows to be returned .
:offset Specifies an integer indicating the offsetfrom where the rows should be returned .
:order Lets you specify the fields to set the orderof returned records .
:readonly Marks the returned records read-only .
:select A SQL SELECT statement, as in SELECT *
FROM items .
-
8/8/2019 Rails Ppt2
32/34
To start the process of displaying the items forsale when the buy controller s index action is
called, you call return_items in the indexaction:class BuyController < A pplicationController
def index@items = Item . return_itemsendend
-
8/8/2019 Rails Ppt2
33/34
Show Database Items in a Web Page
The Store
Buy From Our Store!
Welcome to the store .
Please buy a lot of items, hankyou . -
8/8/2019 Rails Ppt2
34/34