Code Academy Week 10 Notes

Hidden field in a form: f.hidden_field(:list_id)

Nested resources:

resources :students
resources :grades

But is a top-level list of grades any good?

Do this in routes.rb

resources :students do
  resources :grades

1 is :student_id, 4 is :id for grade

It is the default syntax for Rails 3.1 and up

$(function() {


$ ->

For code block, you indent instead of using braces. Indentation is meaningful. So use soft tabs.
We need parentheses if the function takes 0 parameters.

$("#new_item_link").click (e)->

Put in two gems:

gem 'haml'
gem 'haml-rails'
gem 'haml-rails', :group => :development


rails generate scaffold landmark name:string rating:integer

So we get haml:

ericm@finance:~/ruby/codeAcademy/week10/LandmarksApp$ rails generate scaffold landmark name:string rating:integer
      invoke  active_record
      create    db/migrate/20120313152347_create_landmarks.rb
      create    app/models/landmark.rb
      invoke    test_unit
      create      test/unit/landmark_test.rb
      create      test/fixtures/landmarks.yml
       route  resources :landmarks
      invoke  scaffold_controller
      create    app/controllers/landmarks_controller.rb
      invoke    haml
      create      app/views/landmarks
      create      app/views/landmarks/index.html.haml
      create      app/views/landmarks/edit.html.haml
      create      app/views/landmarks/show.html.haml
      create      app/views/landmarks/new.html.haml
      create      app/views/landmarks/_form.html.haml
      invoke    test_unit
      create      test/functional/landmarks_controller_test.rb
      invoke    helper
      create      app/helpers/landmarks_helper.rb
      invoke      test_unit
      create        test/unit/helpers/landmarks_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/
      invoke    scss
      create      app/assets/stylesheets/landmarks.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

In HAML, whitespace is significant
For classes for p tag, you could do this:

Back from break
If you have a list of items with an association:

has_many :items

you can use :dependent with :delete, :delete_all will delete without calling callbacks (like if you have some email actions), :nullify removes foreign key

has_many :items, :dependent => :destroy

Use twitter authentication
Create a sessions controller

rails g controller sessions new create

The create action will be called by Twitter

class SessionsController < ApplicationsController
  def create
    logger.debug "hello"
    # logger.debug request.env['omniauth.auth'].inspect
    auth_data = request.env['omniauth.auth']
    logger.debug "Provider: #{auth_data['Provider']}"


get "sessions/create"

Use the omniauth gem
This gem will add routes to your application.
1. Register your app with twitter at
You get Access Level, Consumer Key, Consumer Secret, Request Token URL, Authorize URL, Access Token URL, Callback URL
We set the callback URL
You need to put in instead of localhost for dev
2. In gem file
You need to add a gem for each “strategy”

gem 'omniauth-twitter'

There is no base omniauth that we need to add.
The strategy gems depend on the base omniauth gem, and they will fetch it.
3. Write an omniauth initializer
in config/initializers: any code there will be run once – no naming standard
Create a file called config/initializers/omniauth.rb
Add this:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :developer unless Rails.env.production?
  provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']

In routes put

match '/auth/:provider/callback', to: 'sessions#create'

match will map to any HTTP verb

In application layout: add a signin link

<%= link_to 'sign in with twitter', "/auth/developer" %>

So your user model could just have a column for Provider and UID
You could get other stuff from hash

They have provider :twitter, ENV[‘TWITTER_KEY’], ENV[‘TWITTER_SECRET’]
environment variables are part of operating system
To view them:
env | sort
in irb, type “ENV”
ENV[“PATH”] is the OS “path” environment variables
So you can add it in the environment. It only works on a specific machine. Good luck deploying to Heroku
In terminal:

export TWITTER_KEY="adhflsblsdfbsdlfhsbdlbf"
export TWITTER_SECRET="gpwpilwopohbnmiuhuftttdfg"

heroku config:add TWITTER_KEY="adhflsblsdfbsdlfhsbdlbf"

print them out:

heroku config

cron tasks:

1. create a file lib/tasks called cron.rake
4. desc “import tweets”
5. task :cron => environment do
7. end
heroku rake cron

    <%= f.collection_select(:runner_id,
                             :name) %>

This is from Entry form. Entry belongs_to runner, runner has many entries
So :runner_id is the field in Runner, id is from runner, name is also from runner
foreign key, table, what to save as the foreign key, what to display