Skip to content

Instantly share code, notes, and snippets.

@julia-
Forked from amysimmons/wdi_week_4_notes.md
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save julia-/6c8ea9d5a6c38de43bad to your computer and use it in GitHub Desktop.

Select an option

Save julia-/6c8ea9d5a6c38de43bad to your computer and use it in GitHub Desktop.

Revisions

  1. @amysimmons amysimmons revised this gist Feb 3, 2015. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -487,6 +487,18 @@ this is so we can view the planets better in the controller

    ###Lab: Single Model CRUD - Oceans or Mountains



    ###Reading: Active Record Helpers

    http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag

    ###Reading: Active Record Migrations

    http://guides.rubyonrails.org/active_record_migrations.html

    You can think of each migration as being a new 'version' of the database. A schema starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries. Active Record knows how to update your schema along this timeline, bringing it from whatever point it is in the history to the latest version.

    ##Wednesday

    ##Thursday
  2. @amysimmons amysimmons revised this gist Feb 3, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -449,9 +449,9 @@ rails generate migration create_whatever

    subl . #fill in the migration

    db :create
    rake db:create

    db :migrate
    rake db:migrate

    touch app/models/whatever.rb

  3. @amysimmons amysimmons revised this gist Feb 3, 2015. 1 changed file with 45 additions and 0 deletions.
    45 changes: 45 additions & 0 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -439,6 +439,51 @@ rails console

    Planet.all

    **Recap of the above steps:**

    rails new something -T

    cd something

    rails generate migration create_whatever

    subl . #fill in the migration

    db :create

    db :migrate

    touch app/models/whatever.rb

    annotate

    rake db:seed

    ---

    http://localhost:3000/info/routes

    will show you the urls and the methods to get to those urls

    ---

    we changed the new planet form so that all the name values were formatted like this - planet[moons]

    this is so we can view the planets better in the controller

    ```
    >> params
    => {"name"=>"", "planet"=>{"image"=>"", "orbit"=>"", "mass"=>"", "diameter"=>"", "distance"=>"", "moons"=>""}, "controller"=>"planets", "action"=>"create"}
    >> params[planet]
    !! #<NameError: undefined local variable or method `planet' for #<PlanetsController:0x007fc52bedecc8>>
    >> params["planet"]
    => {"image"=>"", "orbit"=>"", "mass"=>"", "diameter"=>"", "distance"=>"", "moons"=>""}
    >> params["planet"]["name"]
    => nil
    >> params["planet"].keys
    => ["image", "orbit", "mass", "diameter", "distance", "moons"]
    >>
    ```

    ###Lab: Single Model CRUD - Oceans or Mountains

  4. @amysimmons amysimmons revised this gist Feb 3, 2015. 1 changed file with 211 additions and 0 deletions.
    211 changes: 211 additions & 0 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -229,6 +229,217 @@ end

    ###Single Model CRUD - Planets

    Create path '/'

    Define our database

    Create planets table

    - id integer autoincrement

    - name text

    - mass float

    - moons integer

    - distance integer

    ####The long SQL process

    **Step 1: Setting up the database**

    ```
    rails new solar_systems_app
    ```

    Create planets.sql in db file

    ```
    CREATE TABLE planets (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    image TEXT,
    orbit FLOAT,
    diameter FLOAT,
    distance FLOAT,
    mass FLOAT,
    moons INTEGER
    );
    ```

    Table name must be plural, the model itself must be singular

    create the database by running rake db:create

    it will create a file called development sqlite 3

    we can then cd into the db folder

    run sqlite3 development.sqlite3 < planets.sql in the terminal

    this pushes the planets table into the database

    to test that it worked run sqlite3 development.sqlite3 .schema and it should return the table structure

    **Step 2: Creating a model**

    Now we need to create a model to make this work

    The model goes in the app folder, in a folder called models

    create new file planet.rb

    into that file put

    ```
    class Planet < ActiveRecord::Base
    end
    ```

    gem install annotate

    run annotate

    this inserts a big comment into my planet.rb file which shows the table name and characteristics

    An alternative to pry:

    in order to deal with pry we can type rails console in the terminal

    then we can start running commands like Planet.all

    In order to be in pry rather than console,

    put these into my gemfile

    ```
    group :development do
    gem 'pry-rails'
    gem 'pry-stack_explorer'
    gem 'annotate'
    gem 'quiet_assets'
    gem 'better_errors'
    gem 'binding_of_caller'
    gem 'meta_request'
    end
    ```

    then run bundle

    then run rails console again

    you could do earth = Planet.new

    and then set all the properties

    or you can do this

    venus = Planet.create :name => 'Venus', :mass => 3, :diameter => 11, :distance => 1, :moons => 2

    Planet.destroy_all will destory all planets

    Planet.count should now show zero

    **Step 3: Seed data**

    The seeds.rb file is for seed data

    ```
    Planet.create(:name => 'Earth', :orbit => 1, :moons => 1)
    Planet.create(:name => 'Mars', :orbit => 1.5, :moons => 2)
    Planet.create(:name => 'Venus', :orbit => 0.7, :moons => 0)
    Planet.create(:name => 'Jupiter', :orbit => 3.7, :moons => 7)
    Planet.create(:name => 'Pluto', :orbit => 5, :moons => 3)
    ```

    Enter some seed data and then run rake db:seed in terminal

    This connects the seed data with the database

    to test that it worked, in the console, run Planet.count / Planet.all and the seed data should be there

    if you make a mistake while playing around with your data, you can restore the original seeded data by reseeding them

    first you need to add Planet.destroy_all to the top of the seeds.rb file

    then when you re-run rake db:seed it will first destroy all existing planets, then reseed the original ones

    rake db:drop (deletes the database)

    delete sql file

    so all that's in db folder is the seeds file

    ----

    ####Rails generating the migration for us, without us having to write SQL

    in solar_system_app run

    rails generate migration create_planets

    the rb file should appear in the migrate folder

    it tells rails to create a table

    so this is the equivalent of our sql stuff, but its a lot more rubyish

    ```
    class CreatePlanets < ActiveRecord::Migration
    def change
    create_table :planets do |t|
    end
    end
    end
    ```

    so we want to insert into the do block our table details

    ```
    class CreatePlanets < ActiveRecord::Migration
    def change
    create_table :planets do |t|
    t.string :name
    t.text :image
    t.float :orbit
    t.float :mass
    t.float :diameter
    t.float :distance
    t.integer :moons
    t.timestamps
    end
    end
    end
    ```

    this table works across all databases now

    rake db:create (creates the db)

    rake db:migrate (finds any unapplied migrations and performs them)

    at this point you can run annotate, then go back to the planet.rb file to check thar the table is there

    at this stage we have set up the M part of our MVC model, but we haven't set up any routes

    **Setting up the routes:**

    in the routes.rb file

    get '/planets' => 'planets#index'

    in terminal run rails generate controller planets index

    this means rails will create the file for me and create the views folder and put an index.html.erb file in there ready for me to customize

    rake db:seed

    rails console

    Planet.all


    ###Lab: Single Model CRUD - Oceans or Mountains

    ##Wednesday
  5. @amysimmons amysimmons revised this gist Feb 2, 2015. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -193,6 +193,44 @@ https://gist.github.com/wofockham/ae2479856769f8dbc804

    ##Tuesday

    ###Warmup

    **Decoding messages:**

    https://gist.github.com/epoch/21c0133143f03226cee0

    My crappy solution, but it works!

    ```
    message = "FRZDUGV GLH PDQB WLPHV EHIRUH WKHLU GHDWKV, WKH YDOLDQW QHYHU WDVWH RI GHDWK EXW RQFH."
    message_arr = message.split(//)
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    alphabet_arr = alphabet.split(//)
    alphabet_shift = "DEFGHIJKLMNOPQRSTUVWXYZABC"
    alphabet_shift_arr = alphabet_shift.split(//)
    message_arr.each do |letter|
    if letter != " "
    index = alphabet_shift_arr.index(letter)
    correct_letter = alphabet_arr[index]
    print correct_letter
    else
    print " "
    end
    end
    ```

    ###Single Model CRUD - Planets

    ###Lab: Single Model CRUD - Oceans or Mountains

    ##Wednesday

    ##Thursday
  6. @amysimmons amysimmons revised this gist Feb 2, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -187,9 +187,9 @@ class AutoController < ApplicationController
    end
    ```

    ###Tour
    ###Lab: Movie Stock

    ###Routing and controllers
    https://gist.github.com/wofockham/ae2479856769f8dbc804

    ##Tuesday

  7. @amysimmons amysimmons revised this gist Feb 2, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -171,7 +171,7 @@ root :to => 'pages#home'

    **Debugging:**

    Rather than using binging.pry, use a raise like this:
    Rather than using binding.pry, use a raise like this:

    ```
    class AutoController < ApplicationController
  8. @amysimmons amysimmons revised this gist Feb 2, 2015. 1 changed file with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -165,7 +165,27 @@ Here's how Rails will work when we run the server:

    - We identify the root/home page by including in the routes.rb file

    ```root :to => 'pages#home'```
    ```
    root :to => 'pages#home'
    ```

    **Debugging:**

    Rather than using binging.pry, use a raise like this:

    ```
    class AutoController < ApplicationController
    def color
    raise "Something bad happen"
    raise params.inspect
    end
    def engine
    end
    end
    ```

    ###Tour

  9. @amysimmons amysimmons revised this gist Feb 2, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -163,6 +163,10 @@ Here's how Rails will work when we run the server:

    - Or you can go to localhost:3000/rails/info/routes

    - We identify the root/home page by including in the routes.rb file

    ```root :to => 'pages#home'```

    ###Tour

    ###Routing and controllers
  10. @amysimmons amysimmons revised this gist Feb 2, 2015. 1 changed file with 72 additions and 0 deletions.
    72 changes: 72 additions & 0 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -75,8 +75,12 @@ https://github.com/amysimmons/wdi8_homework/tree/master/amy/blog

    ###Patterns

    Design Patterns: Elements of Reusable Object-Oriented Software (book)

    ###Rails appetiser

    **Movies example:**

    rails new movie_night

    rails server
    @@ -91,6 +95,74 @@ rails server

    localhost:3000/movies

    **Intro example:**

    Rails has three different environments:

    - Development

    - Test

    - Production

    We added the following gems to Gemfile...

    ```
    group :development do
    gem 'pry-rails'
    gem 'pry-stack_explorer'
    gem 'annotate'
    gem 'quiet_assets'
    gem 'better_errors'
    gem 'binding_of_caller'
    gem 'meta_request'
    end
    ```

    ... then ran bundle in the terminal.

    in routes.rb

    ```
    Rails.application.routes.draw do
    get '/home' => 'pages#home'
    # if someone makes a get request to /home
    # go and look in our pages controller and go and find the method called home
    end
    ```

    in pages_controller.rb

    ```
    class PagesController < ApplicationController
    def home
    end
    #we need a home method here because we have a page called /home
    end
    ```

    In views, we created a new folder called pages, which sits alongside layout. We then created a file in the pages folder called home.html.erb

    Here's how Rails will work when we run the server:

    - When I bring up the rails server it will look into my routes.rb file to see what urls are available

    - If someone visits /home, rails will go to the pages controller to find the method

    - If Rails sees that there is no code in the method, it will then go and look for a view that matches the name of this method, so it will find the view called home.html.erb

    **Routes:**

    - Runnign rake routes in the terminal will tell you how many urls are involved in the project

    - Or you can go to localhost:3000/rails/info/routes

    ###Tour

    ###Routing and controllers
  11. @amysimmons amysimmons revised this gist Feb 1, 2015. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -77,6 +77,20 @@ https://github.com/amysimmons/wdi8_homework/tree/master/amy/blog

    ###Rails appetiser

    rails new movie_night

    rails server

    localhost:3000

    rails generate scaffold movie title:string in_theatres:boolean released:date rating:string description:text

    rake db:migrate (updating the database to include the movies table)

    rails server

    localhost:3000/movies

    ###Tour

    ###Routing and controllers
  12. @amysimmons amysimmons revised this gist Feb 1, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -67,6 +67,12 @@ bob.chat("You're a fool")

    ###CRUD demos

    Homework over the weekend was to create a CRUD system.

    I made a blog, with no styling:

    https://github.com/amysimmons/wdi8_homework/tree/master/amy/blog

    ###Patterns

    ###Rails appetiser
  13. @amysimmons amysimmons revised this gist Feb 1, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@

    https://gist.github.com/epoch/ea1798f7269f454f2445

    My solution:
    My solution (exclusing the extension):

    ```
    class Bob
  14. @amysimmons amysimmons created this gist Feb 1, 2015.
    87 changes: 87 additions & 0 deletions wdi_week_4_notes.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    #WDI Week 4 Notes

    ##Monday

    ###Warmup

    **Bob:**

    https://gist.github.com/epoch/ea1798f7269f454f2445

    My solution:

    ```
    class Bob
    def sure
    puts "sure"
    end
    # if you ask him a question
    def whatever
    puts "whatever"
    end
    # if you tell him something
    def chill
    puts "woah, chill out!"
    end
    # if you yell at him
    def fine
    puts "fine, be that way!"
    end
    # if you address him without actually saying anything
    def loser_speak
    puts "loser speak"
    end
    #Start any sentence with "Bro, " and he'll translate the rest of it into l33t sP34k for you.
    def chat(say)
    if say.start_with?"Bro, "
    loser_speak
    elsif say.include? '?'
    sure
    elsif say.empty? || say == " "
    fine
    elsif say == say.upcase
    chill
    else
    whatever
    end
    end
    end
    bob = Bob.new
    bob.chat("Bro, ")
    bob.chat("sup?")
    bob.chat(" ")
    bob.chat("")
    bob.chat("SUP")
    bob.chat("You're a fool")
    ```

    ###CRUD demos

    ###Patterns

    ###Rails appetiser

    ###Tour

    ###Routing and controllers

    ##Tuesday

    ##Wednesday

    ##Thursday

    ##Friday