%td
    .form-control
      = text_field_tag "queue_items[][position]", queue_item.position
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8pWUHvTO4DlVjHuINLlxy8thIn8v8Jm5CfiYYvb1XYQ=", "queue_items"=>[{"position"=>"1"}], "commit"=>"Update Instant Queue"}

In the rails guide:

Combining Them We can mix and match these two concepts. One element of a hash might be an array as in the previous example, or you can have an array of hashes. For example, a form might let you create any number of addresses by repeating the following form fragment

  <input name="addresses[][line1]" type="text"/>
  <input name="addresses[][line2]" type="text"/>
  <input name="addresses[][city]" type="text"/>

This would result in params[:addresses] being an array of hashes with keys line1, line2 and city. Rails decides to start accumulating values in a new hash whenever it encounters an input name that already exists in the current hash.

There’s a restriction, however, while hashes can be nested arbitrarily, only one level of “arrayness” is allowed. Arrays can usually be replaced by hashes; for example, instead of having an array of model objects, one can have a hash of model objects keyed by their id, an array index or some other parameter.

To understand what delegate work, let’s see the following code:

  %td
    = link_to queue_item.video_title, queue_item.video
  %td
    = link_to queue_item.category_name, queue_item.category

Suppose we had some template in the view that we call from our queueitem model. We want the video_title from it. Actually, we can just call queue_item.video.title or queue_item.category.name. But we can make them more friendly by use delegate in the model.

Read more...
Understand Form Builder in Rails

Form Builder

I want to introduce the basic of how to use form builder in rails. If we have a basic title and description page, if we submit the form without no content, we hope it will show some error message. the haml file will looks like this.

  %section.new_todo
    %h3 Add a new todo
    = form_for @todo do |f|
      = f.label :name, "Name"
      = f.text_Filed :name
      = f.label :description, "Description"
      = f.text_area :description, rows: 6
    %br
    = f.submit "Add this Todo"

Of course you can write if @todo.errors?, then render all the errors out.

Read more...
Learning jQuery

This is my short note while learning jQuery. I still left some highlight without contents. But it is as good as an gateway to search for the contents.

why use jQuery

  • easily modify DOM(use css selector)
  • easily modify CSS
  • more animation
  • jQuery can chain function together
  • use AJAX to receive/send data without refresh the page
Read more...

  function foo(x,y) {
    this.x = x;
    this.y = y;
  };

  var bar = new foo;

  bar.__proto__ === foo.prototype // true
  bar.__proto__.proto__ === Object.prototype //true

  foo.__proto__ === Function.prototype //true
  foo.__proto__.__proto__ === Object.prototype // true
  bar instanceof foo
  foo instanceof Function
  bar instanceof Object
Read more...