T-11 to Course Start

Rails for Zombies II

  • Level 3
    • Minimize models per controller, one concern and resource per controller
    • REST: create = post, update = put, destroy = delete
    • rake routes will show the paths you would link_to
    • Relative vs Absolute paths: name_path vs name_url
    • Forms
      • form_for, text_area, check_box, radio_button, select ['one', 'two', 'three']
    • Types
      • password_field, number_field, range_field, email_field, url_field, telephone_field
    • Nested Routes
      • resources :one do ; resources :two ; end
      • <%= form_for([@zombie, @weapon]) do |f| %> yields a weapon form for a zombie
      • square brackets also have some tricky business with paths: <%= link_to weapon.name, [@zombie, weapon] %> gives a path for a weapon belonging to a zombie
    • raw command to not escape user input
    • Rails div_for 
    • View helpers
      • truncate (:length, :separator), pluralize(), .to_sentence, number_to_currency(), number_to_human
    • partials on enumerables – I’m totally lost
  • Level 4
    • sending emails
      • rails g mailer MailerClass email_type1
        • a view that will  represent the email, plus an app/mailer/ directory
      • a method in the app/mailer/ directory will say things like:
        • attachements['name.ext'] = File.read("#{Rails.root}/public/file.ext")
          • no need to read the file if its an image
        • mail to: <refer to email address>, subject: "lala"
          • more commands can be added
        • defaults should be set outside the method definition in the same MailerClass
      • content of email is the view
      • sending the model is a method in a model, and call MailerClass.email_type1
        • add a conditional at the top of the model for Example with: after_save :email_type1_notification, if: :boolean?
          • or place it in a method, and call the mailer method and deliver: MailerClass.email_type1(*args).deliver if #whatever
      • madmimi gem for mailing lists
    • Asset pipeline
      • directories
        • app – specific
        • lib – shared code
        • vendor
      • /assets/ will check all assets folders
      • tags
        • javascript_include_tag, stylesheet_link_tag, image_tag
      • take advantage of caching by appending .erb to your stylesheets, and then referencing them with url(<%= asset_path('button.png') %>)
    • Minification: rake assets:precompile

Javascript

  • Installation
    • installed node.js, this youtube video didn’t work. deleted the build file he asked to create.
    • went through these instructions, first route didn’t work, but second did.
  • Light table is great

Core Javascript (Zakas)

  • declaring a variable: var varname = 5;
    • if defined inside function
      • with var = its destroyed when the function exits
      • without var = global scope
  • typeof var_name gives the Data Type
    • undefined type
      • declaring without initializing (setting equal to anything)… it get the value: undefined
      • never having declared also gets undefined, but… is not the same thing
    • null type
      • null == undefined
      • but null is not the same as never having defined
      • typeof(null) is object
    • boolean type
      • the Boolean() casting function
        • 0, -0, NaN, “”, undefined, null => false
    • floating-point
      • takes twice as much memory
      • e notation is fine
      • Number.MAX_VALUE ~ 2e308, Number.MIN_VALUE = 5E-324. Can check with isFinite(num)
    • NaN
      • not equal to any value, not even itself
      • but isNaN() is true for anything that can’t be converted into a number
    • Strings
      • literals on page 42
    • Object
      • base for all objects, page 45
  • Conversion
    • Number(arg)
      • null, false, empty string, = 0, true = 1, strings work, hexadecimals work. Everything else, i.e.. “a”, => NaN
    • arg.valueOf() is like Number(arg), but all NaN results get a toString(arg)
    • parseInt(arg) tries to find a number at the beginning of the string, and drops everything else, otherwise returns NaN
      • can sometimes surprisingly return an octal literal
      • parseFloat(arg) works the same
    • arg.toString() with arguments – radix? on page 43
    • String(arg), since arg.toString doesn’t work with null and undefinedString(arg) exists, and does. Same as adding ("") to something
  • RegEx
    • text.search(pattern) => position of first match
      • match => array of matches
      • split => non-inclusive split into array
      • replace => replaces first occurrence, gives a string, doesn’t change original
  • Rubylike
    • ++num is like: num.next, but always affects original (side effects)
      • postfix to avoid side effects: num++
      • has power to make strings integers, null/false into 0, true into 1, others into NaN
    • just like in Ruby, can do: if (jon)    alert("jon is real!"); and it will alert as long as jon is not set to something false
  • function starts to define a function
  • make an array: var answer_array = new Array();
    • add into it by index
  • functions written to initialize a new object are called constructors

Try JQuery

  • DOM -document object model
    • Has structure referred to by: parent/child nodes
    • goal – quickly find an html element
      • interaction happens on browser side, jQuery helps deal with multitude of different web browsers
      • needs to happen after html loads: jQuery(document).ready(function(){ //code here ]);
    • Reading
      • find by html tags: $("tag")
        • will find all, not just first
        • find content of tags with methods: $("h1").text
      • or find by css
        • $(".class") or $("#id")
      • descendants:
        • Ex: $(".class li") will do all <li> descendants of that class
        • 1 generation at a time with child selector:
          • $(".class > li") i.e. $("parent > child")
      • multiple items: use commas for intersection
      • methods
        • $(".class li:first") i.e. $(".class li:filter")
        • odd and even work too (index starts at 0)
      • Traversing is faster
        • $(".class li") becomes $(".class").find("");
        • filters: $(".class").last();
        • chaining is fine: $(".class").find().next();
        • parents and children: $(".class").parent(); OR $(".class").children("li")
    • Appending
      • create a node: var name = $('<tag> text </tag>');
      • attach a node: $('.class').append(name)
        • $(‘fetch’).append(new_guy), prepend(new_guy), after(new_guy), before(new_guy)
        • new_guy.appendTo($(‘fetch’)), new_guy.prependTo, new_guy.insertAfter, new_guy.insertBefore
      • .remove() 
  • Getting jQuery
    • load into html file (at bottom near </body>): <script src="jquery.min.js></script> with whatever app.js file you did your work in trailing it

Random

  • Meloni HTML/CSS/Javascript (ch3 bits)
    • selector.class { style_property: value; } (or selector#id)
    • span  = div without line breaks
    • local inline styles override external stylesheets
Advertisements

One thought on “T-11 to Course Start

  1. Pingback: T-10 to Course Start | Chemistry BS/English BA does App Academy

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