REST

representational state transfer

  • its idea is to use HTTP to make calls between machines to get resources
  • return the representation of the resource (i.e., the content type of the resource), not the resource itself.
  • resources :actions is equivalent of defining four name routes to 7 controller actions: index, show, create, update, delete, edit, new
  • both new and edit use GET method
  • index action is usually show plural/collection resource, so is create
  • resouces is actions for members, not collections, but you can add collections inside the resource loop.
  • Example to know the difference between representation and resource == representation: form submission, resources: xml string, html files
  • one URI corresponds to one resource; resource can be any information, one picture, one paragraph, one video
  • representation is realized through respond_to

Learn REST

Routing

  • URL mapping to action by routing in config/route.rb
  • the routing rule created first has higher priority
  • “rake routes” show all your routes
  • “rake routes CONTROLLER=products” show all your routes in productions controller
  • limit http method used to access a route match ‘products/:id’ => ‘products#show’, :via => post which is equivalent to  get ‘products/:id’ => ‘products#show’
  • name a route by :as => ‘my_route_name’ so that you can link_to “MY ROUTE”, my_route_name_path
  • specify a parameter in a link: link_to “MY ROUTE”, my_route_name_path(:id=>item.id)
  • parameter can be objects, which is transformed to a hash by object.to_param in the model

Log

Most programming context in Rails (MVC) have a logger attribute

  • view: <%= debug @user %>
  • controller: logger.debug "The object is #{@object}"
  • controller: return render :text => "The object is #{@object}" The render :text call will dump the text to the screen and halt execution of the current action.
  • Model: logger.debug

How can we watch the log in development

  • look at log/development.log
  • tail -f app_root/log/development.log which will scroll the log as it gets stuff appended to it.
  • -f means monitor a file, which will display the last 10 lines and append new lines to the display as new lines are added to the file
  • to interrupt tail while monitoring, use ctrl+C
  • less -R file -> interpret the color codes and escape sequences.

Debug rails applications

FasterCSV has nothing to do with Rails framework. Is a Ruby library.

Initializer

Inflector

  • transform strings/words from singular to plural
  • add your own rule in config/initilizers/inflections.rb

MIME

  • Multipurpose Internet Mail Extension
  • Internet standard that extends the format of email or APIs to support different content type
  • examples: json, html, xml, yaml

session store

  • set session store in DB: ActionController::Base.session-store=:active_record_store in lib/initilizers/session_store.rb

Log

  • set logger config.logger =? specify a log.
  • By default, each log is created under rail.root/log/environment_name.log
  • Log levels: debug is zero
  • set log_level in config so that all higher or equal level will be logged into your logs.

the role of schema.rb

  • when run tests, Rails dump the schema of your development DB and copies it to the test DB
  • Rails uses the auto-generated script schema.rb as your development DB

Cache

  • config.cache_classes=false, let you don’t have to restart websever everytime you make code changes.
  • This is useful in the development environment

require and load a file

  • when you require a ruby file, the interpreter executes and caches it; If it is required later, the cached one will be used.
  • When you load a ruby file, the interpreter executes the file again, no matter how many times it executed.
  • ruby load files in load path set in initializer.rb

Name convention

  • Rails use name convention to guess which files it should require or load
  • e.g., A::B becomes require “a/b” so you should rarely need to explicitly load Ruby code in your Rails applications (using require) if you follow the name conventions.

mail delivery

  • It is handy to set config.action_mailer.perform_deliveries=false in development. No delivery attempt is performed, but you can still see the mail in the log file to check it look good.

deprecation print

  • print deprecation notice to the stderr
  • config.active_support.deprecation = :stderr in config/environments/tests.rb

lunch bag

nslookup google.com

“foo”.object_id

“foo”.methods

guest = Object.new; def gues.t.name “guest” end

s=”foo”; s<<“d”; s.length => 4

protocol: TCP, HTTP

inheritance include inheritance of implementation and type. But the inheritance of type does not help because ruby is dynamic typing. When we talk about dynamic typing, we talk about a mechanism where the type of a variable can change and be resolved on the fly at the exact moment it gets parsed by the interpreter. When we talk about static typing, we talk about a mechanism where the type of a variable is resolved in advance by the interpreter/compiler. Javascript is dynamically typed too.

A dynamically typed language is a language where the type of a variable can be altered at any time. (It is a string, now it is a Fixnum, now it is a Time object, etc.)

A statically typed language is the opposite. (Decide what x is once for all and don’t change your mind!)

A strongly typed language is a language that is being strict about what you can do with your typed variables. (Don’t mix them… or I will throw you an error in the face!)

A weakly typed language is the opposite. (Do what you want with your different types. Mix them all! We’ll see what happens!)

http://www.rubyfleebie.com/ruby-is-dynamically-and-strongly-typed/

delegation means composition in ruby but means prototypes in Javascript.

validate_with

validate { |r| r.home_phone? || r.cell_phone? } # validate it should be either a home phone or a cell phone.

polymorphism: method lookup is fast, but testing class type is slow and messy.