This document discusses adding additional fields to a dogs database table in Ruby on Rails. It begins by explaining that the existing name field is a string datatype. It then lists some other fields that could be added like color, age, fixed status, and entrance date. For the age field, it recommends the integer datatype. It provides the commands to add the age field to the database and whitelist it in the controller. It also shows how to add a number input field to the form and display the age in the index view. In the end, it prompts the reader to choose the correct datatypes for the remaining fields.
2. 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
4. ...but are missing important info...
color
age
fixed or not?
entrance date
5. 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.
6. Let’s add “age” to “dogs” table
What data type should the age field be?
7. 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
15. 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
18. Check out your app!
Make sure you can add and view dogs’ ages.
19. Congrats! Here’s what we’ve done...
1. added a field in the database to store the age
2. told the controller to accept the :age parameter
3. added a field in the form where we can enter the age
4. added a field in the index to display the age
20. 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 shelter
2. the color of the dog
3. a description of the dog's temperament
4. whether or not the dog is fixed