20
Datatypes RailsBridgeChicago Module 2 Ameeda Chowdhury twitter.com/ameedahc

RBC Mod 2: Datatypes

Embed Size (px)

Citation preview

Page 1: RBC Mod 2: Datatypes

Datatypes

RailsBridgeChicago Module 2Ameeda Chowdhurytwitter.com/ameedahc

Page 2: RBC Mod 2: Datatypes

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

Page 3: RBC Mod 2: Datatypes

Dogs currently only have names…..

Page 4: RBC Mod 2: Datatypes

...but are missing important info...coloragefixed or not?entrance date

Page 5: RBC Mod 2: Datatypes

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.

Page 6: RBC Mod 2: Datatypes

Let’s add “age” to “dogs” table

What data type should the age field be?

Page 7: RBC Mod 2: Datatypes

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

Page 8: RBC Mod 2: Datatypes

Commands to add “age”

Page 9: RBC Mod 2: Datatypes

Note: “age” NOT in the “dogs” table yet

Page 10: RBC Mod 2: Datatypes

Migrate the database to add “age”

Page 11: RBC Mod 2: Datatypes

Now “age” is a field of “dogs” table

Page 12: RBC Mod 2: Datatypes

STOP! You can’t update “age” yet ....

… Because “age” isn’t whitelisted in the DogsController, only “name” is /app/controllers/dogscontroller.rb

Page 13: RBC Mod 2: Datatypes

Whitelist “age” /app/controllers/dogscontroller.rb

Add :age to the permit list

Page 14: RBC Mod 2: Datatypes

The form doesn’t have an “age” field

Page 15: RBC Mod 2: Datatypes

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

Page 16: RBC Mod 2: Datatypes

Display each dog’s ageedit /app/views/dogs/index.html.erb

Page 17: RBC Mod 2: Datatypes

Commit changes!

Commit to git

Page 18: RBC Mod 2: Datatypes

Check out your app!

Make sure you can add and view dogs’ ages.

Page 19: RBC Mod 2: Datatypes

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

Page 20: RBC Mod 2: Datatypes

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