Upload
ameedahc
View
104
Download
0
Embed Size (px)
Citation preview
Datatypes
RailsBridgeChicago Module 2Ameeda Chowdhurytwitter.com/ameedahc
Dog table stores a dog’s name only
name is a field in the dogs table, and it’s defined as of data type string.
Because the name field is defined as of data type string, this field can only hold values that can be put in quotes i.e. a dog can be anmed “Hello world!” and “Frankie” and “57”, but NOT 57 or :Frankie or 7.55
Dogs currently only have names…..
...but are missing important info...coloragefixed or not?entrance date
Recall: Adding “name” to “dogs” table
When we first generated our "dogs" table, we put "name:string" in our generating code – this created for us a field in our "dogs" table called "name" that stores "string" values.
Let’s add “age” to “dogs” table
What data type should the age field be?
Possible data types for fields:primary_key :string:text:integer:float:decimal :datetime:time:date:binary:boolean
An integer (as you may remember from math class) is any number without a fractional part – e.g. 1, 0, 205, -53, etc.
A float, on the other hand, is any number that can have a fractional part - e.g. 1.75, 0.5, 1.0
Commands to add “age”
Note: “age” NOT in the “dogs” table yet
Migrate the database to add “age”
Now “age” is a field of “dogs” table
STOP! You can’t update “age” yet ....
… Because “age” isn’t whitelisted in the DogsController, only “name” is /app/controllers/dogscontroller.rb
Whitelist “age” /app/controllers/dogscontroller.rb
Add :age to the permit list
The form doesn’t have an “age” field
Add a number field to the form/app/views/dogs/form.html.erb
numberfield is a Rails form helper; this creates a field that accepts numbers in the form the user sees in their browser
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html lists and explains the various form helpers available in Rails
/app/views/dogs/form.html.erb
Display each dog’s ageedit /app/views/dogs/index.html.erb
Commit changes!
Commit to git
Check out your app!
Make sure you can add and view dogs’ ages.
Congrats! Here’s what we’ve done...1. added a field in the database to store the age2. told the controller to accept the :age parameter3. added a field in the form where we can enter the age4. added a field in the index to display the age
Now, try it on your own...Choose the right data type for each remaining field for the dogs table and add them to the table.
1. the date a dog arrived in the shelter2. the color of the dog3. a description of the dog's temperament4. whether or not the dog is fixed