T-4 until assessment00 window closes

Rails for Zombies 1

  • Level 1
    • interesting goings on with hashes:
      • we make an object belonging to a class Tweet, and Rails lowercases and pluralizes it to tweets and looks in our database for such a table
        • lookup is via an id that Rails creates and sets for us
    • Commands used:
      • on objects retrieved from a hash
        • update_attributes
        • Reading: order(:hash_attribute), limit(number) … for more checkout their slides
  • Level 2 – Relationships
    • how do we map something like class access ( zom4 = Zombie.find(3) ) to a hash table? – ActiveRecord
      • Rails comes with a ton of validations
    • linking tables of hashes: belongs_to :class and has_many :classes (where the class is lowercase, and note the change in singular/plural case)
  • Level 3 – layouts/ERB
    • ERB reminder: <% %> Evaluate, <%= %> evaluate and print result
    • /app/views/layouts/ contains the html wrapper, with a header and footer.
      • contains a <%= yield %> that tells rails to look for /app/views/classname/show.html.erb which contains the core ruby code
      • also could contain <%= stylesheet_link_tag %> that looks in /public/stylesheets
      • also could contain <%= javascript_link_tag %> that looks in /public/javascripts
      • also could contain <%= csrf_meta_tag %> for security
      • About public directory – whenever anyone edits the URL, controller will first try to display a matching file in the public directory before executing the request with rails
    • link_to 
  • Level 4 – /app/controllers/
    • keep model calls in the controller – minimize Ruby code in views
      • declare an instance variable inside the controller, and refer to it in the view
    • before_filter :method_name, :only => [:dependent_method1, :dependent_method2]  – will run method_name at the start of any method in the array, like dependent_method1
    • Rails has defaults for displaying in xml ( do render :xml => @thing_tobe_rendered )… json is done exactly the same way
    • lots of default actions in the view (index, show, new, edit, create, update, destroy)
  • Level 5 – Routing
    • /config/routes.rb contains resources :class – a RESTful resource
      • match 'custom_url_path' => "classname#existing_method"  if we wanted a custom URL to lead to the same page as an existing method, like say, creating a new class object
        • i.e. match 'path' => "controller#action"
        • Note: lowercase controller or classname
        • AND, if want to link to this elsewhere like: <%= link_to "Displayed link name", custom_url_path_or_watevs_path%>
          • match 'custom_url_path' => "ClassName#existing_method" , :as => "custom_url_path_or_watevs"
      • Redirect: match '/thing_to_match' => redirect('/<local path or a link to elsewhere>')
        • Note: start with slash
      • Routing the root: root :to => "class#method" i.e  root :to => "controller#action" 
        • in a link: <%= link_to "All Tweets", root_path %>
      • Grabbing from URL – say, a zipcode
        • match 'still_url/:zipcode' => "controller#action"
          • referenced by params[:zip code] in the controller
          • can also tack on a: :as => "custom_url_path_or_watevs"  to name the route

Rails Tutorial Chapter 2

  • Drafting – 1st step is to create a data model (representation of structured needed by the application)
  • Scaffolding
    • rails generate scaffold User name:string email:string
    • rails generate scaffold Micropost content:string user_id:integer
    • both update the config/routes.rb file with new rules, and create their own files in app/controllers/
  • Database Migration (using Rake)
    • updates database with the model created when scaffolding
    • bundle exec rake db:migrate
      • bundle before exec rake makes sure that the version of rake in the Gemfile is used
      • About Rake – just like make was used on Unix systems since forever ago to compile code, rake is Ruby make
  • Mapping of user requests to controller actions happens in config/routes.rb
    • these controller actions are in app/controllers/users_controller.rb for example:
      1. it asks the Users model to retrieve all the users from the database and places them in its own instance variable
      2. this variable appears in app/models/user.rb (in a class that inherits from ActiveRecord)
      3. then the controller calls the View in app/views/users/index.html.erb which outputs HTML with the help of ERB (introduced in EventManager)
      4. the controller returns this view to the browser
  • REST is about creating resources that will get created, deleted, updated, and read (POST, DELETE, PUT, GET – the 4 main HTTP request methods – also CRUD)
  • Validations
    • are put in the models: app/models/
    • validates :content, :length => { :maximum => 140 }

  • Associations between data models are made possible by ActiveRecord
    • our classes in our models app/models/ inherit from ActiveRecord::Base
    • classes in our controllers app/controllers/ inherit from ApplicationController (like all rails controllers)
      • app/controllers/application_controller.rb shows that ApplicationController inherits from ActionController::Base
  • Interact with rails via rails console

Rails Tutorial Chapter 3

  • Working with RSpec – rails new app_name --skip-test-unit will not generate the default test directory
    • as a result, the Gemfile this time will include a gem for RSpec and for the RSpec library specific to Rails (rspec-rails), and also a gem that helps simulate a user’s interaction the the application (capybara)
    • rails generate rspec:install 
    • integration tests
      • known as request specs a la RSpec
      • simulate actions of a user
      • Capybara (cucumber is an alternative)
    • unit tests
    • Creating a test
      • rails generate integration_test static_pages adds to the /spec/requests/ directory
      • some syntax is from capybara: visit '/static_pages/home' and the page.should have_content('Sample App')
        • the should have_content is flexible enough to be case insensitive and be okay with trailing or leading characters on the content string
      • to run the test: bundle exec spec spec/requests/static_pages_spec.rb
  • Adding a new heroku app: initially: heroku create --stack cedar && git push heroku master , and later, coupled with git: git push && git push heroku – heroku logs could help if errors are run into
  • Git notes
    • git commit --amend to edit the last commit message
    • git checkout -b name makes a new branch
  • Static Page generation: rails generate controller StaticPages home help --no-test-framework , not using Rspec yet
    • this makes corresponding rules in the /config/routes.rb file for the actions home and help
      • get "static_pages/home" in the /config/routes.rb maps requests for the URL to the home action in the StaticPages controller
      • the get routes to HTTP
    • BTW
      • to undo generate: same thing but with destroy
      • database migrations like: rake db:migrate will be undone with rake db:rollback
    • Generating a static page later…
      • add a route to it in /config/routes.rb
      • add the respective method/action for it in its class/controller
      • add the right view for it, method_name.html.erb
  • Controller
    • an action’s method in its controller file might be empty, but it does something. What happens: Visiting a page, look in the controller, execute code in the home action, and then render the view.
    • this means an action like home has a corresponding view called home.html.erb

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s