Hack hack hack...

An open journal-- some of it written for you, but most of it is for me.

FIS Day18

POTD

  • Edsgar Dijkstra

    • inventor of structured programming
    • came up with “for, while, until, case”
    • computer science is not about machines and equipment management
  • If you use more of a bad tool, it won’t fix it.

    About the use of language; it is impossible to sharpen a pencil with a blunt axe. It is equally vain to try to do it with ten blunt axes instead.

  • Programming is just about communicating clearly.

  • Once it is teachable it is concrete.

Ruby Tidbit

Semi-Colon Deliminates a Line
1
2
def create;end
#the semi-colon deliminates the info on two lines

Rails Lecture

link to unless current
1
<%= link_to_unless_current %> #this is slick for nav!

Routes

Routes File
1
2
3
4
5
match '/name' => 'songs#name'
#not as specific and therefore as good as
get '/name' => 'songs#name'

get 'site/about' #rails will assume that site is the controller and about is the action
Generate a Controller
1
2
rails generate controller CreditCard open debit credit close
#this will create a credit_card_controller with 4 actions

??have to delete rm-rf public/index.html

Rendering Templates
1
2
3
4
5
6
7
8
class Sessions Controller < ApplicationController

render :action
render :template
#in practice
render 'under_construction' #this is the action in the sessions controller
render 'shared/under_construction' #this is a full relative path
#and more instructional of where to find the template

Private and public methods

Private Method e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Dog
  def bark
    case stomach_volume
    when :very
      puts "woof woof!"
    when :not_at_all
      puts "woof"
    end
  end

private #it would also break if this was protected.

  def stomach_volume
    :very
  end
end

fido = Dog.new
fido.stomach_volume #this is not going to work.

private, public, and protected all create keyword scope -> created limited, predicted interfaces

if a method is private it can only be called on an implicit receiver- can’t call call on an explicit receiver

protected -> can only be called on self.

When Private Methods Become Useful in the Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class SomeController

def new
  under_construction
end

def create
  under_constructions
end

private

def under_construction
  render 'shared/under_construction'
end

end

In the migration

  • def change for when the reverse can be assumed
  • def up and down for things like renaming columns -> because rails wouldn’t be able to infer what the reverse would be.

  • t.timestamps - is a created at and updated at

Signups

when you login, you are creating a session between you and the website. when you signup you are creating a user on their system– that lives in their DB

Routes
1
2
get '/signup' => 'users#new'
post '/signup' => 'users#create'

Mass assignment => when you assign initialization properties during assingment.

  • Migrations

    • define and edit schema
  • Models -ORM

    • insert rows /update
    • database
  • Routes maps URLs to actions in the controller

  • controllers

    • contains the business logic
  • views

    • the actual interface

-> In sinatra the routes are built into controllers.

This must be how form_for works
1
2
3
4
def form_for(obj, &block)
  f = FormBuilder.new(obj)
  yield f
end

In your controller helper…

Rolling My Own Form Builder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%= form_for(@song, :builder = > SongHelper::SongForm) %>
#in the controller helper

module SongsHelper
  class SongForm < ActionView::Helpers::FormBuilder

    def full_name_field
      label(:first_name) +
      text_field :first_name

      label(:last_name) +
      text_field :last_name
    end
  end
end

Customizing the forms and encapsulate in the controller helpers…

Test::Unit to RSpec

change out the libraries add rspec to the gem file rip out test folder

rails g install rspec

changing from minitest to rspec or

“when i generated the scaffold, it generated the specs for me…”

Posting Info to an object

  1. instantiate an instance
  2. collect the form data
  3. and you populate the instance attribute with the proper data

If I want to use form_for I need an active record instance.

Read this…

Understand parameter naming conventions

Comments