SlideShare a Scribd company logo
The 11th Round of ROR Lab.




Rails Form Helpers

        April 21th, 2012

        Hyoseong Choi
          ROR Lab.
Short Review


               ROR Lab.
Eager Loading
         Associations
- Conditions on Eager Loaded Associations -

      conditional ā€œjoinsā€ > conditional ā€œincludesā€


   Post.includes(:comments)
     .where("comments.visible", true)


  SELECT "posts"."id" AS t0_r0, ... "comments"."updated_at" AS t1_r5
  FROM "posts"
  LEFT OUTER JOIN "comments"
  ON "comments"."post_id" = "posts"."id"
  WHERE (comments.visible = 1)


                                                                       ROR Lab.
Eager Loading
         Associations
- Conditions on Eager Loaded Associations -
                      INNER JOIN                             LEFT OUTER JOIN
      conditional ā€œjoinsā€ > conditional ā€œincludesā€


   Post.includes(:comments)
     .where("comments.visible", true)


  SELECT "posts"."id" AS t0_r0, ... "comments"."updated_at" AS t1_r5
  FROM "posts"
  LEFT OUTER JOIN "comments"
  ON "comments"."post_id" = "posts"."id"
  WHERE (comments.visible = 1)


                                                                       ROR Lab.
Scopes
             Passing in arguments
class Post < ActiveRecord::Base
Ā Ā scope :1_week_before,
  lambda { |time| where("created_at < ?", time) }
end




class Post < ActiveRecord::Base
Ā Ā def self.1_week_before(time)
Ā Ā Ā Ā where("created_at < ?", time)
Ā Ā end
                      ***What about ā€œas a class methodā€ ?

                                                     ROR Lab.
Scopes
                   Passing in arguments
      class Post < ActiveRecord::Base
      Ā Ā scope :1_week_before,
        lambda { |time| where("created_at < ?", time) }
      end




           le
       eab
    erclass Post < ActiveRecord::Base
  ef
pr    Ā Ā def self.1_week_before(time)
      Ā Ā Ā Ā where("created_at < ?", time)
      Ā Ā end
                            ***What about ā€œas a class methodā€ ?

                                                           ROR Lab.
Form Helpers


               ROR Lab.
form_tag

ā€¢ Two arguments
 1. path for action
 2. options hash
   ā€¢ submission method
   ā€¢ HTML options : class, ...
                                 ROR Lab.
form_tag
                        Basic Form

<%= form_tag do %>
Ā Ā Form contents
<% end %>




<form accept-charset="UTF-8" action="/home/index" method="post">
Ā Ā <div style="margin:0;padding:0">
Ā Ā Ā Ā <input name="utf8" type="hidden" value="&#x2713;" />
Ā Ā Ā Ā <input name="authenticity_token" type="hidden"
value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
Ā Ā </div>
Ā Ā Form contents
</form>




                                                                   ROR Lab.
form_tag
                 Multiple Hashes

form_tag(:controller => "people", :action => "search", :method
=> "get", :class => "nifty_form")

# => '<form accept-charset="UTF-8" action="/people/search?




form_tag({:controller => "people", :action => "search"}, :method
=> "get", :class => "nifty_form")

# => '<form accept-charset="UTF-8" action="/people/search"



                                                           ROR Lab.
Helpers
        Generating Form Elements
ā€¢   check_box_tag
ā€¢   radio_button_tag
ā€¢   text_area_tag
ā€¢   password_ļ¬eld_tag
ā€¢   hidden_ļ¬eld_tag
ā€¢   search_ļ¬eld_tag
ā€¢   telephone_ļ¬eld_tag
ā€¢   url_ļ¬eld_tag
ā€¢   email_ļ¬eld_tag


                                   ROR Lab.
Helpers
        Generating Form Elements
ā€¢   check_box_tag
ā€¢   radio_button_tag
ā€¢   text_area_tag
ā€¢   password_ļ¬eld_tag
ā€¢   hidden_ļ¬eld_tag
ā€¢   search_ļ¬eld_tag
ā€¢   telephone_ļ¬eld_tag
                         HTML5 controls
ā€¢   url_ļ¬eld_tag
ā€¢   email_ļ¬eld_tag


                                   ROR Lab.
Helpers
        Generating Form Elements
ā€¢   check_box_tag
ā€¢   radio_button_tag
ā€¢   text_area_tag
ā€¢   password_ļ¬eld_tag
ā€¢   hidden_ļ¬eld_tag         polyļ¬ller
ā€¢   search_ļ¬eld_tag
ā€¢   telephone_ļ¬eld_tag
                         HTML5 controls
ā€¢   url_ļ¬eld_tag
ā€¢   email_ļ¬eld_tag


                                        ROR Lab.
Helpers
        Generating Form Elements
ā€¢   check_box_tag
ā€¢   radio_button_tag
ā€¢   text_area_tag
ā€¢   password_ļ¬eld_tag
ā€¢   hidden_ļ¬eld_tag         polyļ¬ller
ā€¢   search_ļ¬eld_tag
ā€¢   telephone_ļ¬eld_tag
                         HTML5 controls
ā€¢   url_ļ¬eld_tag
ā€¢   email_ļ¬eld_tag


                                        ROR Lab.
Helpers
        Generating Form Elements
ā€¢   check_box_tag
ā€¢   radio_button_tag
ā€¢   text_area_tag           yepnope
ā€¢   password_ļ¬eld_tag
ā€¢   hidden_ļ¬eld_tag         polyļ¬ller
ā€¢   search_ļ¬eld_tag
ā€¢   telephone_ļ¬eld_tag
                         HTML5 controls
ā€¢   url_ļ¬eld_tag
ā€¢   email_ļ¬eld_tag


                                        ROR Lab.
Helpers
        Generating Form Elements
ā€¢   check_box_tag
ā€¢   radio_button_tag       Modernizr
ā€¢   text_area_tag           yepnope
ā€¢   password_ļ¬eld_tag
ā€¢   hidden_ļ¬eld_tag         polyļ¬ller
ā€¢   search_ļ¬eld_tag
ā€¢   telephone_ļ¬eld_tag
                         HTML5 controls
ā€¢   url_ļ¬eld_tag
ā€¢   email_ļ¬eld_tag


                                        ROR Lab.
HTML 5
               Form Helpers
   Rails 3                HTML5 Input Types
 search_ļ¬eld                        search
telephone_ļ¬eld                        tel
   url_ļ¬eld                           url
  email_ļ¬eld                         email
 number_ļ¬eld                       number
  range_ļ¬eld                        range
                   Agile Web Development with Rails 4th edition


                                                        ROR Lab.
Model Object
  Helpers




               ROR Lab.
Model Object
  Helpers

  ā€¢   check_box_tag




                      ROR Lab.
Model Object
  Helpers

  ā€¢   check_box_tag
  ā€¢   radio_button_tag




                         ROR Lab.
Model Object
  Helpers

  ā€¢   check_box_tag
  ā€¢   radio_button_tag
  ā€¢   text_area_tag




                         ROR Lab.
Model Object
  Helpers

  ā€¢   check_box_tag
  ā€¢   radio_button_tag
  ā€¢   text_area_tag
  ā€¢   password_ļ¬eld_tag




                          ROR Lab.
Model Object
  Helpers

  ā€¢   check_box_tag
  ā€¢   radio_button_tag
  ā€¢   text_area_tag
  ā€¢   password_ļ¬eld_tag
  ā€¢   hidden_ļ¬eld_tag




                          ROR Lab.
Model Object
      Helpers

            ā€¢   check_box_tag
            ā€¢   radio_button_tag
ā€¢ No _tag   ā€¢   text_area_tag
            ā€¢   password_ļ¬eld_tag
            ā€¢   hidden_ļ¬eld_tag




                                    ROR Lab.
Model Object
      Helpers

            ā€¢   check_box_tag
            ā€¢   radio_button_tag
ā€¢ No _tag   ā€¢   text_area_tag
            ā€¢   password_ļ¬eld_tag
            ā€¢   hidden_ļ¬eld_tag




                                    ROR Lab.
for Model Object
         @person -> name : ā€œHenryā€


<%= text_ļ¬eld(:person, :name) %>




<input id="person_name" name="person[name]" type="text"




                                                          ROR Lab.
for Model Object
         @person -> name : ā€œHenryā€


<%= text_ļ¬eld(:person, :name) %>




<input id="person_name" name="person[name]" type="text"




                                                          ROR Lab.
for Model Object
         @person -> name : ā€œHenryā€


<%= text_ļ¬eld(:person, :name) %>




<input id="person_name" name="person[name]" type="text"




                                                          ROR Lab.
Binding a Form to
    an Object
def new
Ā Ā @article = Article.new
end




<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




                                                            ROR Lab.
Binding a Form to
    an Object
def new
Ā Ā @article = Article.new
end




<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




                                                            ROR Lab.
Binding a Form to
    an Object
def new
Ā Ā @article = Article.new
end




<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




                                                            ROR Lab.
Binding a Form to
    an Object
def new
Ā Ā @article = Article.new
end




<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




                                                            ROR Lab.
Binding a Form to
    an Object
def new
Ā Ā @article = Article.new
end




<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




                                                            ROR Lab.
Binding a Form to
    an Object
def new
Ā Ā @article = Article.new
end




<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




                                                            ROR Lab.
Binding a Form to
    an Object
def new
Ā Ā @article = Article.new
end




<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




                                                            ROR Lab.
<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




<form accept-charset="UTF-8" action="/articles/create"
method="post" class="nifty_form">
Ā Ā <input id="article_title" name="article[title]" size="30"
type="text" />
Ā Ā <textarea id="article_body" name="article[body]" cols="60"
rows="12"></textarea>
Ā Ā <input name="commit" type="submit" value="Create" />
</form>




                                                               ROR Lab.
<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




       params[:article]
<form accept-charset="UTF-8" action="/articles/create"
method="post" class="nifty_form">
Ā Ā <input id="article_title" name="article[title]" size="30"
       params[:article][:title]
type="text" />
Ā Ā <textarea id="article_body" name="article[body]" cols="60"
rows="12"></textarea>
Ā Ā <input name="commit" type="submit" value="Create" />
</form>params[:article][:body]

                                                               ROR Lab.
<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




       params[:article]
<form accept-charset="UTF-8" action="/articles/create"
method="post" class="nifty_form">
Ā Ā <input id="article_title" name="article[title]" size="30"
       params[:article][:title]
type="text" />
Ā Ā <textarea id="article_body" name="article[body]" cols="60"
rows="12"></textarea>
Ā Ā <input name="commit" type="submit" value="Create" />
</form>params[:article][:body]

                                                               ROR Lab.
<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




       params[:article]
<form accept-charset="UTF-8" action="/articles/create"
method="post" class="nifty_form">
Ā Ā <input id="article_title" name="article[title]" size="30"
       params[:article][:title]
type="text" />
Ā Ā <textarea id="article_body" name="article[body]" cols="60"
rows="12"></textarea>
Ā Ā <input name="commit" type="submit" value="Create" />
</form>params[:article][:body]

                                                               ROR Lab.
<%= form_for @article, :url => { :action => "create" }, :html =>
{:class => "nifty_form"} do |f| %>
Ā Ā <%= f.text_ļ¬eld :title %>
Ā Ā <%= f.text_area :body, :size => "60x12" %>
Ā Ā <%= f.submit "Create" %>
<% end %>




       params[:article]
<form accept-charset="UTF-8" action="/articles/create"
method="post" class="nifty_form">
Ā Ā <input id="article_title" name="article[title]" size="30"
       params[:article][:title]
type="text" />
Ā Ā <textarea id="article_body" name="article[body]" cols="60"
rows="12"></textarea>
Ā Ā <input name="commit" type="submit" value="Create" />
</form>params[:article][:body]

                                                               ROR Lab.
ļ¬elds_for


Dry Lab in Terminal




                      ROR Lab.
ļ¬elds_for
Person
  ā€¢name
  ā€¢age
  ā€¢sex
  ā€¢address
  ā€¢telephone


                     ROR Lab.
ļ¬elds_for
Person         Contact
  ā€¢name
  ā€¢age
  ā€¢sex
  ā€¢address
  ā€¢telephone


                         ROR Lab.
ļ¬elds_for
Person          Contact
  ā€¢name           ā€¢address
  ā€¢age            ā€¢telephone
  ā€¢sex




                          ROR Lab.
ļ¬elds_for
           has_one
Person               Contact
  ā€¢name                ā€¢address
  ā€¢age                 ā€¢telephone
  ā€¢sex




                               ROR Lab.
ļ¬elds_for
           has_one
Person                Contact
  ā€¢name                 ā€¢address
  ā€¢age    belong_to     ā€¢telephone
  ā€¢sex




                                ROR Lab.
ļ¬elds_for
                   has_one
Person                                  Contact
  ā€¢name                                   ā€¢address
  ā€¢age           belong_to                ā€¢telephone
  ā€¢sex
class Person < ActiveRecord::Base
 has_one :contact, :dependent => :destroy
end

class Contact < ActiveRecord::Base
 belongs_to :person



                                                  ROR Lab.
Using Resources
   resources :articles in conļ¬g/routes.rb


## Creating a new article
# long-style:
form_for(@article, :url => articles_path)
# same thing, short-style (record identiļ¬cation gets used):
form_for(@article)
Ā 
## Editing an existing article
# long-style:
form_for(@article, :url => article_path(@article), :html =>
{ :method => "put" })
# short-style:
form_for(@article)




                                                              ROR Lab.
Using Namespaces
namespace :admin do                  in routes.rb
Ā Ā resources :posts, :comments
end




form_for [:admin, @post]        in _form.html.erb




                                           ROR Lab.
HTTP Verbs

ā€¢ GET
           for all browsers
ā€¢ POST
ā€¢ PUT             ?
ā€¢ DELETE
                              ROR Lab.
HTTP Verbs

ā€¢ GET
           for all browsers
ā€¢ POST
ā€¢ PUT             ?
ā€¢ DELETE
                              ROR Lab.
HTTP Verbs

ā€¢ GET
           for all browsers
ā€¢ POST
ā€¢ PUT             ?
ā€¢ DELETE
                              ROR Lab.
PUT & DELETE
       ā€¢ A hidden input : _method
form_tag(search_path, :method => "put")




<form accept-charset="UTF-8" action="/search" method="post">
Ā Ā <div style="margin:0;padding:0">
Ā Ā Ā Ā <input name="_method" type="hidden" value="put" />
Ā Ā Ā Ā <input name="utf8" type="hidden" value="&#x2713;" />
Ā Ā Ā Ā <input name="authenticity_token" type="hidden"
value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
Ā Ā </div>
Ā Ā ...




                                                               ROR Lab.
select_tag
<select name="city_id" id="city_id">
Ā Ā <option value="1">Lisbon</option>
Ā Ā <option value="2">Madrid</option>
Ā Ā ...
Ā Ā <option value="12">Berlin</option>
</select>




<%= select_tag(:city_id, '<option value="1">Lisbon</option>...')
%>




                                                           ROR Lab.
options_for_select
 <%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...]) %>
 Ā 
 output:                  1st argument => a nested array
 Ā 
 <option value="1">Lisbon</option>
 <option value="2">Madrid</option>
 ...




 <%= select_tag(:city_id, options_for_select(...)) %>




                                                           ROR Lab.
options_for_select
 <%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %>
 Ā 
 output:
 Ā               2nd argument => ā€˜selectedā€™
 <option value="1">Lisbon</option>
 <option value="2" selected="selected">Madrid</option>




                                                                 ROR Lab.
options_for_select
 <%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %>
 Ā 
 output:
 Ā               2nd argument => ā€˜selectedā€™
 <option value="1">Lisbon</option>
 <option value="2" selected="selected">Madrid</option>




                                                                 ROR Lab.
_tag
              select_tag
# controller:
@person = Person.new(:city_id => 2)




# view:
<%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %>




                                                                   ROR Lab.
select_tag ORM
         for
# controller:
@person = Person.new(:city_id => 2)




# view:
<%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %>




                                                                   ROR Lab.
select_tag ORM
         for
# controller:
@person = Person.new(:city_id => 2)




# view:
<%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %>



                               2
                               pre-selected by Rails



                                                                   ROR Lab.
select_tag ORM
                        for
               # controller:
               @person = Person.new(:city_id => 2)




               # view:
               <%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %>
form builder




                                                2
                                                pre-selected by Rails
               # select on a form builder
               <%= f.select(:city_id, ...) %>


                                                                                  ROR Lab.
select from
            collection
<% cities_array = City.all.map { |city| [city.name, city.id] } %>
<%= options_for_select(cities_array) %>




   options _for_select




                                                                    ROR Lab.
select from
            collection
<% cities_array = City.all.map { |city| [city.name, city.id] } %>
<%= options_for_select(cities_array) %>




   options _from_collection                       _for_select
                                   (City.all, :id, :name)




                                                                    ROR Lab.
collection_select

<%= collection_select




<%= f.collection_select




                          ROR Lab.
collection_select

<%= collection_
    collection_select




<%= f.collection_select




                          ROR Lab.
collection_select

<%= collection_
    collection_select




<%= f.
    f.collection_select




                          ROR Lab.
Special Select
ā€¢ time_zone_select
  : time_zone_options_for_select
ā€¢ country_select
  : isolated to country_select plugin
ā€¢ select_date : barebones helper
ā€¢ date_select : model object helper
                                        ROR Lab.
select_date
<%= select_date Date.today, :preļ¬x => :start_date %>




<select id="start_date_year"
     name="start_date[year]"> ... </select>
<select id="start_date_month"
     name="start_date[month]"> ... </select>
<select id="start_date_day"
     name="start_date[day]"> ... </select>




                                                       ROR Lab.
select_date
<%= select_date Date.today, :preļ¬x => :start_date %>




<select id="start_date_year"
     name="start_date[year]"> ... </select>
<select id="start_date_month"
     name="start_date[month]"> ... </select>
<select id="start_date_day"
     name="start_date[day]"> ... </select>




Date.civil(params[:start_date][:year].to_i, params[:start_date]
[:month].to_i, params[:start_date][:day].to_i)




                                                              ROR Lab.
date_select
<%= date_select :person, :birth_date %>




<select id="person_birth_date_1i"
     name="person[birth_date(1i)]"> ... </select>
<select id="person_birth_date_2i"
     name="person[birth_date(2i)]"> ... </select>
<select id="person_birth_date_3i"




                                                    ROR Lab.
date_select
<%= date_select :person, :birth_date %>




<select id="person_birth_date_1i"
     name="person[birth_date(1i)]"> ... </select>
<select id="person_birth_date_2i"
     name="person[birth_date(2i)]"> ... </select>
<select id="person_birth_date_3i"




{:person => {'birth_date(1i)' => '2008', 'birth_date(2i)' => '11',




                                                               ROR Lab.
Individual Selects
ā€¢ select_year
ā€¢ select_month
ā€¢ select_day      :preļ¬x defaults to ā€œdateā€
ā€¢ select_hour
ā€¢ select_minute
ā€¢ select_second

                                   ROR Lab.
Individual Selects
ā€¢ select_year
ā€¢ select_month
ā€¢ select_day      :preļ¬x defaults to ā€œdateā€
ā€¢ select_hour
ā€¢ select_minute
ā€¢ select_second

                                   ROR Lab.
Uploading Files
params[:picture]
 <%= form_tag({:action => :upload}, :multipart => true) do %>
 Ā Ā <%= ļ¬le_ļ¬eld_tag 'picture' %>
 <% end %>
 Ā 
 <%= form_for @person
                          , :multipart => true
 Ā Ā <%= f.ļ¬le_ļ¬eld :picture %>                    do |f| %>
 <% end %>


params[:person][:picture]
Since Rails 3.1, it automatically sets the multipart/
form-data with ļ¬le_ļ¬eld in the form_for
                                                          ROR Lab.
Uploading Files
params[:picture]
 <%= form_tag({:action => :upload}, :multipart => true) do %>
 Ā Ā <%= ļ¬le_ļ¬eld_tag 'picture' %>
 <% end %>
 Ā 
 <%= form_for @person
 Ā Ā <%= f.ļ¬le_ļ¬eld :picture %> |f| %>
                           do
 <% end %>


params[:person][:picture]
Since Rails 3.1, it automatically sets the multipart/
form-data with ļ¬le_ļ¬eld in the form_for
                                                          ROR Lab.
Uploading Files
params[:picture]
 <%= form_tag({:action => :upload}, :multipart => true) do %>
 Ā Ā <%= ļ¬le_ļ¬eld_tag 'picture' %>
 <% end %>
 Ā 
 <%= form_for @person
 Ā Ā <%= f.ļ¬le_ļ¬eld :picture %> |f| %>
                           do
 <% end %>


params[:person][:picture]
Since Rails 3.1, it automatically sets the multipart/
form-data with ļ¬le_ļ¬eld in the form_for
                                                          ROR Lab.
What gets
             uploaded
def upload
Ā Ā uploaded_io = params[:person][:picture]
Ā Ā File.open(Rails.root.join('public', 'uploads',
uploaded_io.original_ļ¬lename), 'w') do |ļ¬le|
Ā Ā Ā Ā ļ¬le.write(uploaded_io.read)
Ā Ā end




                                                   ROR Lab.
What gets
             uploaded
def upload
Ā Ā uploaded_io = params[:person][:picture]
Ā Ā File.open(Rails.root.join('public', 'uploads',
uploaded_io.original_ļ¬lename), 'w') do |ļ¬le|
Ā Ā Ā Ā ļ¬le.write(uploaded_io.read)
Ā Ā end




an instance of a subclass of IO
    ā€¢ original_ļ¬lename
    ā€¢ content_type : MIME type
                                                   ROR Lab.
Upload Gems


ā€¢ CarrierWave
ā€¢ Paperclip


                  ROR Lab.
Ajaxing Upload

ā€¢ ā€œremotipartā€ gem
  : AJAX style ļ¬le uploads with jQuery

  https://github.com/leppert/remotipart




                                          ROR Lab.
Customizing Form
    Builders




              ROR Lab.
Customizing Form
    Builders

<%= form_for @person do |f| %>
Ā Ā <%= text_ļ¬eld_with_label f, :ļ¬rst_name %>




                                              ROR Lab.
Customizing Form
    Builders
<%= form_for @person do |f| %>
Ā Ā <%= text_ļ¬eld_with_label f, :ļ¬rst_name %>




<%= form_for @person, :builder => LabellingFormBuilder do |f|
%>
Ā Ā <%= f.text_ļ¬eld :ļ¬rst_name %>




class LabellingFormBuilder < ActionView::Helpers::FormBuilder
Ā Ā def text_ļ¬eld(attribute, options={})
Ā Ā Ā Ā label(attribute) + super
Ā Ā end
end

                                                           ROR Lab.
Simple_form


ā€¢ https://github.com/plataformatec/
  simple_form




                                      ROR Lab.
Params Naming
  Client                   Server

    Form
for model obj
 ā€œ@personā€      params[:person]

   submit




                                    ROR Lab.
Params Naming
  Client           Server

    Form
for model obj
 ā€œ@personā€      params[:person]

   submit




                             ROR Lab.
Basic Structures
                  Arrays & Hashes

<input id="person_name" name="person[name]"

    type="text" value="Henry"/>


params hash
            {ā€˜personā€™ => {ā€˜nameā€™ => ā€˜Henryā€™}}

params[:person]
          {ā€˜nameā€™ => ā€˜Henryā€™}

params[:person][:name]
        ā€˜Henryā€™
                                                ROR Lab.
Nested Hashes
<input id="person_address_city"
    name="person[address][city]"
    type="text" value="New York"/>



params hash
        {'person' => {'address' => {'city' => 'New York'}}}




                                                              ROR Lab.
Duplicated Params
 <input name="person[phone_number][]" type="text"/>
 <input name="person[phone_number][]" type="text"/>
 <input name="person[phone_number][]" type="text"/>




params[:person][:phone_number]
        [
            ā€™02-333-1234ā€™,
            ā€˜031-323-9898ā€™,
            ā€˜062-546-2323ā€™
        ]
                                                      ROR Lab.
Duplicated Params
 <input name="person[phone_number][]" type="text"/>
 <input name="person[phone_number][]" type="text"/>
 <input name="person[phone_number][]" type="text"/>




params[:person][:phone_number]
        [
            ā€™02-333-1234ā€™,
            ā€˜031-323-9898ā€™,
            ā€˜062-546-2323ā€™
        ]
                                                      ROR Lab.
Duplicated Params
 <input name="person[phone_number][]" type="text"/>
 <input name="person[phone_number][]" type="text"/>
 <input name="person[phone_number][]" type="text"/>




params[:person][:phone_number]
        [
            ā€™02-333-1234ā€™,
            ā€˜031-323-9898ā€™,
            ā€˜062-546-2323ā€™
        ]
                                                      ROR Lab.
Duplicated Params
 <input name="person[phone_number][]" type="text"/>
 <input name="person[phone_number][]" type="text"/>
 <input name="person[phone_number][]" type="text"/>




params[:person][:phone_number]
        [
            ā€™02-333-1234ā€™,
            ā€˜031-323-9898ā€™,
            ā€˜062-546-2323ā€™
        ]
                                                      ROR Lab.
Mixed Params
   hash nth-nested but array only one-level

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



params[:addresses]
       [
           {
               ā€˜line1ā€™ => ā€™02-333-1234ā€™,
               ā€˜line2ā€™ => ā€˜031-323-9898ā€™,
               ā€˜cityā€™ => ā€˜seoulā€™
           }
       ]
                                                 ROR Lab.
Using Form
               Helpers
<%= form_for @person do |person_form| %>
Ā Ā <%= person_form.text_ļ¬eld :name %>
Ā Ā <% @person.addresses.each do |address| %>
Ā Ā Ā Ā <%= person_form.ļ¬elds_for address, :index => address do |
address_form|%>
Ā Ā Ā Ā Ā Ā <%= address_form.text_ļ¬eld :city %>
Ā Ā Ā Ā <% end %>
Ā Ā <% end %>
<% end %>



<form accept-charset="UTF-8" action="/people/1" class="edit_person" id="edit_person_1"
method="post">
Ā Ā <input id="person_name" name="person[name]" size="30" type="text" />
Ā Ā <input id="person_address_23_city" name="person[address][23][city]" size="30" type="text" />
Ā Ā <input id="person_address_45_city" name="person[address][45][city]" size="30" type="text" />
</form>




                                                                                            ROR Lab.
Using Form
             Helpers
{
    'person' =>
    {
      'name' => 'Bob',
      'address' =>
      {
        '23' => {'city' => 'Paris'},
        '45' => {'city' => 'London'}
      }
    }




                                       ROR Lab.
Using :index

<%= ļ¬elds_for 'person[address][primary]', address, :index =>
address do |address_form| %>
Ā Ā <%= address_form.text_ļ¬eld :city %>




<%= ļ¬elds_for 'person[address][primary][]', address do |
address_form| %>
Ā Ā <%= address_form.text_ļ¬eld :city %>




                                                           ROR Lab.
Form to External
  Resources - 1
<%= form_tag 'http://farfar.away/form', :authenticity_token =>
'external_token') do %>
Ā Ā Form contents




<%= form_tag 'http://farfar.away/form', :authenticity_token =>
false) do %>
Ā Ā Form contents




                                                          ROR Lab.
Form to External
  Resources - 1
<%= form_tag 'http://farfar.away/form', :authenticity_token =>
'external_token') do %>
Ā Ā Form contents
                               payment gateway

<%= form_tag 'http://farfar.away/form', :authenticity_token =>
false) do %>
Ā Ā Form contents




                                                          ROR Lab.
Form to External
  Resources - 1
<%= form_tag 'http://farfar.away/form', :authenticity_token =>
'external_token') do %>
Ā Ā Form contents




<%= form_tag 'http://farfar.away/form', :authenticity_token =>
false) do %>
Ā Ā Form contents
                      payment gateway


                                                          ROR Lab.
Form to External
  Resources - 2
<%= form_for @invoice, :url =>
external_url, :authenticity_token => 'external_token' do |f|
Ā Ā Form contents
<% end %>




<%= form_for @invoice, :url =>
external_url, :authenticity_token => false do |f|
Ā Ā Form contents




                                                               ROR Lab.
Form to External
  Resources - 2
<%= form_for @invoice, :url =>
     form_for
external_url, :authenticity_token => 'external_token' do |f|
Ā Ā Form contents
<% end %>




<%= form_for @invoice, :url =>
     form_for
external_url, :authenticity_token => false do |f|
Ā Ā Form contents




                                                               ROR Lab.
Complex forms
Railscasts by Ryan Bates




                           ROR Lab.
Complex forms
Railscasts by Ryan Bates

ā€¢   Complex Forms Part 1 - Episode #73

ā€¢   Complex Forms Part II - Episode #74

ā€¢   Complex Forms Part III - Episode #75

ā€¢   Nested Model Form (resivsed) - Episode #196


                                           ROR Lab.
ź°ģ‚¬ķ•©ė‹ˆė‹¤.

More Related Content

Viewers also liked

ė ˆģ¼ģŠ¤ź°€ģ“ė“œ ķ•œźø€ė²ˆģ—­ ź³µź°œķ”„ė”œģ ķŠø RORLabGuides ģ†Œź°œ
ė ˆģ¼ģŠ¤ź°€ģ“ė“œ ķ•œźø€ė²ˆģ—­ ź³µź°œķ”„ė”œģ ķŠø RORLabGuides ģ†Œź°œė ˆģ¼ģŠ¤ź°€ģ“ė“œ ķ•œźø€ė²ˆģ—­ ź³µź°œķ”„ė”œģ ķŠø RORLabGuides ģ†Œź°œ
ė ˆģ¼ģŠ¤ź°€ģ“ė“œ ķ•œźø€ė²ˆģ—­ ź³µź°œķ”„ė”œģ ķŠø RORLabGuides ģ†Œź°œRORLAB
Ā 
Getting Started with Rails (3)
Getting Started with Rails (3) Getting Started with Rails (3)
Getting Started with Rails (3) RORLAB
Ā 
Active Support Core Extension (3)
Active Support Core Extension (3)Active Support Core Extension (3)
Active Support Core Extension (3)RORLAB
Ā 
Getting Started with Rails (1)
Getting Started with Rails (1)Getting Started with Rails (1)
Getting Started with Rails (1)RORLAB
Ā 
Getting Started with Rails (4)
Getting Started with Rails (4) Getting Started with Rails (4)
Getting Started with Rails (4) RORLAB
Ā 
Action Controller Overview, Season 1
Action Controller Overview, Season 1Action Controller Overview, Season 1
Action Controller Overview, Season 1RORLAB
Ā 

Viewers also liked (6)

ė ˆģ¼ģŠ¤ź°€ģ“ė“œ ķ•œźø€ė²ˆģ—­ ź³µź°œķ”„ė”œģ ķŠø RORLabGuides ģ†Œź°œ
ė ˆģ¼ģŠ¤ź°€ģ“ė“œ ķ•œźø€ė²ˆģ—­ ź³µź°œķ”„ė”œģ ķŠø RORLabGuides ģ†Œź°œė ˆģ¼ģŠ¤ź°€ģ“ė“œ ķ•œźø€ė²ˆģ—­ ź³µź°œķ”„ė”œģ ķŠø RORLabGuides ģ†Œź°œ
ė ˆģ¼ģŠ¤ź°€ģ“ė“œ ķ•œźø€ė²ˆģ—­ ź³µź°œķ”„ė”œģ ķŠø RORLabGuides ģ†Œź°œ
Ā 
Getting Started with Rails (3)
Getting Started with Rails (3) Getting Started with Rails (3)
Getting Started with Rails (3)
Ā 
Active Support Core Extension (3)
Active Support Core Extension (3)Active Support Core Extension (3)
Active Support Core Extension (3)
Ā 
Getting Started with Rails (1)
Getting Started with Rails (1)Getting Started with Rails (1)
Getting Started with Rails (1)
Ā 
Getting Started with Rails (4)
Getting Started with Rails (4) Getting Started with Rails (4)
Getting Started with Rails (4)
Ā 
Action Controller Overview, Season 1
Action Controller Overview, Season 1Action Controller Overview, Season 1
Action Controller Overview, Season 1
Ā 

Similar to Active Record Form Helpers, Season 1

Solr's Search Relevancy (Understand Solr's query debug)
Solr's Search Relevancy (Understand Solr's query debug)Solr's Search Relevancy (Understand Solr's query debug)
Solr's Search Relevancy (Understand Solr's query debug)Wongnai
Ā 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2RORLAB
Ā 
Prairie DevCon 2015 - Crafting Evolvable API Responses
Prairie DevCon 2015 - Crafting Evolvable API ResponsesPrairie DevCon 2015 - Crafting Evolvable API Responses
Prairie DevCon 2015 - Crafting Evolvable API Responsesdarrelmiller71
Ā 
Mongo and Harmony
Mongo and HarmonyMongo and Harmony
Mongo and HarmonySteve Smith
Ā 
Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2RORLAB
Ā 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Railsrstankov
Ā 
Django Overview
Django OverviewDjango Overview
Django OverviewBrian Tol
Ā 
Intro to Rails ActiveRecord
Intro to Rails ActiveRecordIntro to Rails ActiveRecord
Intro to Rails ActiveRecordMark Menard
Ā 
Api's and ember js
Api's and ember jsApi's and ember js
Api's and ember jsEdwin Cruz
Ā 
mashraqi_farhan
mashraqi_farhanmashraqi_farhan
mashraqi_farhanwebuploader
Ā 
Let's read code: python-requests library
Let's read code: python-requests libraryLet's read code: python-requests library
Let's read code: python-requests librarySusan Tan
Ā 
Open Social In The Enterprise
Open Social In The EnterpriseOpen Social In The Enterprise
Open Social In The EnterpriseTim Moore
Ā 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Michelangelo van Dam
Ā 
Routing 1, Season 1
Routing 1, Season 1Routing 1, Season 1
Routing 1, Season 1RORLAB
Ā 
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperTame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperGiordano Scalzo
Ā 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialYi-Ting Cheng
Ā 
Test driven development with behat and silex
Test driven development with behat and silexTest driven development with behat and silex
Test driven development with behat and silexDionyshs Tsoumas
Ā 
Ruby on rails
Ruby on rails Ruby on rails
Ruby on rails Mohit Jain
Ā 
TDD with phpspec2
TDD with phpspec2TDD with phpspec2
TDD with phpspec2Anton Serdyuk
Ā 

Similar to Active Record Form Helpers, Season 1 (20)

Solr's Search Relevancy (Understand Solr's query debug)
Solr's Search Relevancy (Understand Solr's query debug)Solr's Search Relevancy (Understand Solr's query debug)
Solr's Search Relevancy (Understand Solr's query debug)
Ā 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2
Ā 
Prairie DevCon 2015 - Crafting Evolvable API Responses
Prairie DevCon 2015 - Crafting Evolvable API ResponsesPrairie DevCon 2015 - Crafting Evolvable API Responses
Prairie DevCon 2015 - Crafting Evolvable API Responses
Ā 
Mongo and Harmony
Mongo and HarmonyMongo and Harmony
Mongo and Harmony
Ā 
Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2Getting started with Rails (2), Season 2
Getting started with Rails (2), Season 2
Ā 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
Ā 
Django Overview
Django OverviewDjango Overview
Django Overview
Ā 
Intro to Rails ActiveRecord
Intro to Rails ActiveRecordIntro to Rails ActiveRecord
Intro to Rails ActiveRecord
Ā 
Api's and ember js
Api's and ember jsApi's and ember js
Api's and ember js
Ā 
mashraqi_farhan
mashraqi_farhanmashraqi_farhan
mashraqi_farhan
Ā 
Let's read code: python-requests library
Let's read code: python-requests libraryLet's read code: python-requests library
Let's read code: python-requests library
Ā 
Open Social In The Enterprise
Open Social In The EnterpriseOpen Social In The Enterprise
Open Social In The Enterprise
Ā 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12
Ā 
Routing 1, Season 1
Routing 1, Season 1Routing 1, Season 1
Routing 1, Season 1
Ā 
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperTame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
Ā 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
Ā 
Test driven development with behat and silex
Test driven development with behat and silexTest driven development with behat and silex
Test driven development with behat and silex
Ā 
behat
behatbehat
behat
Ā 
Ruby on rails
Ruby on rails Ruby on rails
Ruby on rails
Ā 
TDD with phpspec2
TDD with phpspec2TDD with phpspec2
TDD with phpspec2
Ā 

More from RORLAB

Getting Started with Rails (2)
Getting Started with Rails (2)Getting Started with Rails (2)
Getting Started with Rails (2)RORLAB
Ā 
Self join in active record association
Self join in active record associationSelf join in active record association
Self join in active record associationRORLAB
Ā 
Asset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsAsset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsRORLAB
Ā 
Active Support Core Extension (2)
Active Support Core Extension (2)Active Support Core Extension (2)
Active Support Core Extension (2)RORLAB
Ā 
Active Support Core Extensions (1)
Active Support Core Extensions (1)Active Support Core Extensions (1)
Active Support Core Extensions (1)RORLAB
Ā 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2RORLAB
Ā 
Action View Form Helpers - 2, Season 2
Action View Form Helpers - 2, Season 2Action View Form Helpers - 2, Season 2
Action View Form Helpers - 2, Season 2RORLAB
Ā 
Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2RORLAB
Ā 
ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2RORLAB
Ā 
Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2RORLAB
Ā 
Active Record Association (2), Season 2
Active Record Association (2), Season 2Active Record Association (2), Season 2
Active Record Association (2), Season 2RORLAB
Ā 
ActiveRecord Association (1), Season 2
ActiveRecord Association (1), Season 2ActiveRecord Association (1), Season 2
ActiveRecord Association (1), Season 2RORLAB
Ā 
ActiveRecord Callbacks & Observers, Season 2
ActiveRecord Callbacks & Observers, Season 2ActiveRecord Callbacks & Observers, Season 2
ActiveRecord Callbacks & Observers, Season 2RORLAB
Ā 
ActiveRecord Validations, Season 2
ActiveRecord Validations, Season 2ActiveRecord Validations, Season 2
ActiveRecord Validations, Season 2RORLAB
Ā 
Rails Database Migration, Season 2
Rails Database Migration, Season 2Rails Database Migration, Season 2
Rails Database Migration, Season 2RORLAB
Ā 
Getting started with Rails (4), Season 2
Getting started with Rails (4), Season 2Getting started with Rails (4), Season 2
Getting started with Rails (4), Season 2RORLAB
Ā 
Getting started with Rails (3), Season 2
Getting started with Rails (3), Season 2Getting started with Rails (3), Season 2
Getting started with Rails (3), Season 2RORLAB
Ā 
Getting started with Rails (1), Season 2
Getting started with Rails (1), Season 2Getting started with Rails (1), Season 2
Getting started with Rails (1), Season 2RORLAB
Ā 
Routing 2, Season 1
Routing 2, Season 1Routing 2, Season 1
Routing 2, Season 1RORLAB
Ā 
Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1RORLAB
Ā 

More from RORLAB (20)

Getting Started with Rails (2)
Getting Started with Rails (2)Getting Started with Rails (2)
Getting Started with Rails (2)
Ā 
Self join in active record association
Self join in active record associationSelf join in active record association
Self join in active record association
Ā 
Asset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on RailsAsset Pipeline in Ruby on Rails
Asset Pipeline in Ruby on Rails
Ā 
Active Support Core Extension (2)
Active Support Core Extension (2)Active Support Core Extension (2)
Active Support Core Extension (2)
Ā 
Active Support Core Extensions (1)
Active Support Core Extensions (1)Active Support Core Extensions (1)
Active Support Core Extensions (1)
Ā 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2
Ā 
Action View Form Helpers - 2, Season 2
Action View Form Helpers - 2, Season 2Action View Form Helpers - 2, Season 2
Action View Form Helpers - 2, Season 2
Ā 
Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2
Ā 
ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2
Ā 
Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2
Ā 
Active Record Association (2), Season 2
Active Record Association (2), Season 2Active Record Association (2), Season 2
Active Record Association (2), Season 2
Ā 
ActiveRecord Association (1), Season 2
ActiveRecord Association (1), Season 2ActiveRecord Association (1), Season 2
ActiveRecord Association (1), Season 2
Ā 
ActiveRecord Callbacks & Observers, Season 2
ActiveRecord Callbacks & Observers, Season 2ActiveRecord Callbacks & Observers, Season 2
ActiveRecord Callbacks & Observers, Season 2
Ā 
ActiveRecord Validations, Season 2
ActiveRecord Validations, Season 2ActiveRecord Validations, Season 2
ActiveRecord Validations, Season 2
Ā 
Rails Database Migration, Season 2
Rails Database Migration, Season 2Rails Database Migration, Season 2
Rails Database Migration, Season 2
Ā 
Getting started with Rails (4), Season 2
Getting started with Rails (4), Season 2Getting started with Rails (4), Season 2
Getting started with Rails (4), Season 2
Ā 
Getting started with Rails (3), Season 2
Getting started with Rails (3), Season 2Getting started with Rails (3), Season 2
Getting started with Rails (3), Season 2
Ā 
Getting started with Rails (1), Season 2
Getting started with Rails (1), Season 2Getting started with Rails (1), Season 2
Getting started with Rails (1), Season 2
Ā 
Routing 2, Season 1
Routing 2, Season 1Routing 2, Season 1
Routing 2, Season 1
Ā 
Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1
Ā 

Recently uploaded

Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
Ā 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfPo-Chuan Chen
Ā 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptxmansk2
Ā 
INU_CAPSTONEDESIGN_į„‡į…µį„†į…µį†Æį„‡į…„į†«į„’į…©486_į„‹į…„į†øį„…į…©į„ƒį…³į„‹į…­į†¼ į„‡į…”į†Æį„‘į…­į„Œį…”į„…į…­.pdf
INU_CAPSTONEDESIGN_į„‡į…µį„†į…µį†Æį„‡į…„į†«į„’į…©486_į„‹į…„į†øį„…į…©į„ƒį…³į„‹į…­į†¼ į„‡į…”į†Æį„‘į…­į„Œį…”į„…į…­.pdfINU_CAPSTONEDESIGN_į„‡į…µį„†į…µį†Æį„‡į…„į†«į„’į…©486_į„‹į…„į†øį„…į…©į„ƒį…³į„‹į…­į†¼ į„‡į…”į†Æį„‘į…­į„Œį…”į„…į…­.pdf
INU_CAPSTONEDESIGN_į„‡į…µį„†į…µį†Æį„‡į…„į†«į„’į…©486_į„‹į…„į†øį„…į…©į„ƒį…³į„‹į…­į†¼ į„‡į…”į†Æį„‘į…­į„Œį…”į„…į…­.pdfbu07226
Ā 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
Ā 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFVivekanand Anglo Vedic Academy
Ā 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
Ā 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfVivekanand Anglo Vedic Academy
Ā 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfDr. M. Kumaresan Hort.
Ā 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxssuserbdd3e8
Ā 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
Ā 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
Ā 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
Ā 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
Ā 
50 Đį»€ LUYį»†N THI IOE Lį»šP 9 - NĂM Hį»ŒC 2022-2023 (CƓ LINK HƌNH, FILE AUDIO Vƀ ĐƁ...
50 Đį»€ LUYį»†N THI IOE Lį»šP 9 - NĂM Hį»ŒC 2022-2023 (CƓ LINK HƌNH, FILE AUDIO Vƀ ĐƁ...50 Đį»€ LUYį»†N THI IOE Lį»šP 9 - NĂM Hį»ŒC 2022-2023 (CƓ LINK HƌNH, FILE AUDIO Vƀ ĐƁ...
50 Đį»€ LUYį»†N THI IOE Lį»šP 9 - NĂM Hį»ŒC 2022-2023 (CƓ LINK HƌNH, FILE AUDIO Vƀ ĐƁ...Nguyen Thanh Tu Collection
Ā 
Danh saĢch HSG BoĢ£Ģ‚ moĢ‚n caĢ‚Ģp truĢ›oĢ›Ģ€ng - CaĢ‚Ģp THPT.pdf
Danh saĢch HSG BoĢ£Ģ‚ moĢ‚n caĢ‚Ģp truĢ›oĢ›Ģ€ng - CaĢ‚Ģp THPT.pdfDanh saĢch HSG BoĢ£Ģ‚ moĢ‚n caĢ‚Ģp truĢ›oĢ›Ģ€ng - CaĢ‚Ģp THPT.pdf
Danh saĢch HSG BoĢ£Ģ‚ moĢ‚n caĢ‚Ģp truĢ›oĢ›Ģ€ng - CaĢ‚Ģp THPT.pdfQucHHunhnh
Ā 
GIƁO ƁN Dįŗ Y THƊM (Kįŗ¾ HOįŗ CH BƀI BUį»”I 2) - TIįŗ¾NG ANH 8 GLOBAL SUCCESS (2 Cį»˜T) N...
GIƁO ƁN Dįŗ Y THƊM (Kįŗ¾ HOįŗ CH BƀI BUį»”I 2) - TIįŗ¾NG ANH 8 GLOBAL SUCCESS (2 Cį»˜T) N...GIƁO ƁN Dįŗ Y THƊM (Kįŗ¾ HOįŗ CH BƀI BUį»”I 2) - TIįŗ¾NG ANH 8 GLOBAL SUCCESS (2 Cį»˜T) N...
GIƁO ƁN Dįŗ Y THƊM (Kįŗ¾ HOįŗ CH BƀI BUį»”I 2) - TIįŗ¾NG ANH 8 GLOBAL SUCCESS (2 Cį»˜T) N...Nguyen Thanh Tu Collection
Ā 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
Ā 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
Ā 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfSpecial education needs
Ā 

Recently uploaded (20)

Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Ā 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Ā 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
Ā 
INU_CAPSTONEDESIGN_į„‡į…µį„†į…µį†Æį„‡į…„į†«į„’į…©486_į„‹į…„į†øį„…į…©į„ƒį…³į„‹į…­į†¼ į„‡į…”į†Æį„‘į…­į„Œį…”į„…į…­.pdf
INU_CAPSTONEDESIGN_į„‡į…µį„†į…µį†Æį„‡į…„į†«į„’į…©486_į„‹į…„į†øį„…į…©į„ƒį…³į„‹į…­į†¼ į„‡į…”į†Æį„‘į…­į„Œį…”į„…į…­.pdfINU_CAPSTONEDESIGN_į„‡į…µį„†į…µį†Æį„‡į…„į†«į„’į…©486_į„‹į…„į†øį„…į…©į„ƒį…³į„‹į…­į†¼ į„‡į…”į†Æį„‘į…­į„Œį…”į„…į…­.pdf
INU_CAPSTONEDESIGN_į„‡į…µį„†į…µį†Æį„‡į…„į†«į„’į…©486_į„‹į…„į†øį„…į…©į„ƒį…³į„‹į…­į†¼ į„‡į…”į†Æį„‘į…­į„Œį…”į„…į…­.pdf
Ā 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
Ā 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
Ā 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
Ā 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
Ā 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
Ā 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
Ā 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
Ā 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Ā 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Ā 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Ā 
50 Đį»€ LUYį»†N THI IOE Lį»šP 9 - NĂM Hį»ŒC 2022-2023 (CƓ LINK HƌNH, FILE AUDIO Vƀ ĐƁ...
50 Đį»€ LUYį»†N THI IOE Lį»šP 9 - NĂM Hį»ŒC 2022-2023 (CƓ LINK HƌNH, FILE AUDIO Vƀ ĐƁ...50 Đį»€ LUYį»†N THI IOE Lį»šP 9 - NĂM Hį»ŒC 2022-2023 (CƓ LINK HƌNH, FILE AUDIO Vƀ ĐƁ...
50 Đį»€ LUYį»†N THI IOE Lį»šP 9 - NĂM Hį»ŒC 2022-2023 (CƓ LINK HƌNH, FILE AUDIO Vƀ ĐƁ...
Ā 
Danh saĢch HSG BoĢ£Ģ‚ moĢ‚n caĢ‚Ģp truĢ›oĢ›Ģ€ng - CaĢ‚Ģp THPT.pdf
Danh saĢch HSG BoĢ£Ģ‚ moĢ‚n caĢ‚Ģp truĢ›oĢ›Ģ€ng - CaĢ‚Ģp THPT.pdfDanh saĢch HSG BoĢ£Ģ‚ moĢ‚n caĢ‚Ģp truĢ›oĢ›Ģ€ng - CaĢ‚Ģp THPT.pdf
Danh saĢch HSG BoĢ£Ģ‚ moĢ‚n caĢ‚Ģp truĢ›oĢ›Ģ€ng - CaĢ‚Ģp THPT.pdf
Ā 
GIƁO ƁN Dįŗ Y THƊM (Kįŗ¾ HOįŗ CH BƀI BUį»”I 2) - TIįŗ¾NG ANH 8 GLOBAL SUCCESS (2 Cį»˜T) N...
GIƁO ƁN Dįŗ Y THƊM (Kįŗ¾ HOįŗ CH BƀI BUį»”I 2) - TIįŗ¾NG ANH 8 GLOBAL SUCCESS (2 Cį»˜T) N...GIƁO ƁN Dįŗ Y THƊM (Kįŗ¾ HOįŗ CH BƀI BUį»”I 2) - TIįŗ¾NG ANH 8 GLOBAL SUCCESS (2 Cį»˜T) N...
GIƁO ƁN Dįŗ Y THƊM (Kįŗ¾ HOįŗ CH BƀI BUį»”I 2) - TIįŗ¾NG ANH 8 GLOBAL SUCCESS (2 Cį»˜T) N...
Ā 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
Ā 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Ā 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
Ā 

Active Record Form Helpers, Season 1

  • 1. The 11th Round of ROR Lab. Rails Form Helpers April 21th, 2012 Hyoseong Choi ROR Lab.
  • 2. Short Review ROR Lab.
  • 3. Eager Loading Associations - Conditions on Eager Loaded Associations - conditional ā€œjoinsā€ > conditional ā€œincludesā€ Post.includes(:comments) .where("comments.visible", true) SELECT "posts"."id" AS t0_r0, ... "comments"."updated_at" AS t1_r5 FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE (comments.visible = 1) ROR Lab.
  • 4. Eager Loading Associations - Conditions on Eager Loaded Associations - INNER JOIN LEFT OUTER JOIN conditional ā€œjoinsā€ > conditional ā€œincludesā€ Post.includes(:comments) .where("comments.visible", true) SELECT "posts"."id" AS t0_r0, ... "comments"."updated_at" AS t1_r5 FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE (comments.visible = 1) ROR Lab.
  • 5. Scopes Passing in arguments class Post < ActiveRecord::Base Ā Ā scope :1_week_before, lambda { |time| where("created_at < ?", time) } end class Post < ActiveRecord::Base Ā Ā def self.1_week_before(time) Ā Ā Ā Ā where("created_at < ?", time) Ā Ā end ***What about ā€œas a class methodā€ ? ROR Lab.
  • 6. Scopes Passing in arguments class Post < ActiveRecord::Base Ā Ā scope :1_week_before, lambda { |time| where("created_at < ?", time) } end le eab erclass Post < ActiveRecord::Base ef pr Ā Ā def self.1_week_before(time) Ā Ā Ā Ā where("created_at < ?", time) Ā Ā end ***What about ā€œas a class methodā€ ? ROR Lab.
  • 7. Form Helpers ROR Lab.
  • 8. form_tag ā€¢ Two arguments 1. path for action 2. options hash ā€¢ submission method ā€¢ HTML options : class, ... ROR Lab.
  • 9. form_tag Basic Form <%= form_tag do %> Ā Ā Form contents <% end %> <form accept-charset="UTF-8" action="/home/index" method="post"> Ā Ā <div style="margin:0;padding:0"> Ā Ā Ā Ā <input name="utf8" type="hidden" value="&#x2713;" /> Ā Ā Ā Ā <input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" /> Ā Ā </div> Ā Ā Form contents </form> ROR Lab.
  • 10. form_tag Multiple Hashes form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form") # => '<form accept-charset="UTF-8" action="/people/search? form_tag({:controller => "people", :action => "search"}, :method => "get", :class => "nifty_form") # => '<form accept-charset="UTF-8" action="/people/search" ROR Lab.
  • 11. Helpers Generating Form Elements ā€¢ check_box_tag ā€¢ radio_button_tag ā€¢ text_area_tag ā€¢ password_ļ¬eld_tag ā€¢ hidden_ļ¬eld_tag ā€¢ search_ļ¬eld_tag ā€¢ telephone_ļ¬eld_tag ā€¢ url_ļ¬eld_tag ā€¢ email_ļ¬eld_tag ROR Lab.
  • 12. Helpers Generating Form Elements ā€¢ check_box_tag ā€¢ radio_button_tag ā€¢ text_area_tag ā€¢ password_ļ¬eld_tag ā€¢ hidden_ļ¬eld_tag ā€¢ search_ļ¬eld_tag ā€¢ telephone_ļ¬eld_tag HTML5 controls ā€¢ url_ļ¬eld_tag ā€¢ email_ļ¬eld_tag ROR Lab.
  • 13. Helpers Generating Form Elements ā€¢ check_box_tag ā€¢ radio_button_tag ā€¢ text_area_tag ā€¢ password_ļ¬eld_tag ā€¢ hidden_ļ¬eld_tag polyļ¬ller ā€¢ search_ļ¬eld_tag ā€¢ telephone_ļ¬eld_tag HTML5 controls ā€¢ url_ļ¬eld_tag ā€¢ email_ļ¬eld_tag ROR Lab.
  • 14. Helpers Generating Form Elements ā€¢ check_box_tag ā€¢ radio_button_tag ā€¢ text_area_tag ā€¢ password_ļ¬eld_tag ā€¢ hidden_ļ¬eld_tag polyļ¬ller ā€¢ search_ļ¬eld_tag ā€¢ telephone_ļ¬eld_tag HTML5 controls ā€¢ url_ļ¬eld_tag ā€¢ email_ļ¬eld_tag ROR Lab.
  • 15. Helpers Generating Form Elements ā€¢ check_box_tag ā€¢ radio_button_tag ā€¢ text_area_tag yepnope ā€¢ password_ļ¬eld_tag ā€¢ hidden_ļ¬eld_tag polyļ¬ller ā€¢ search_ļ¬eld_tag ā€¢ telephone_ļ¬eld_tag HTML5 controls ā€¢ url_ļ¬eld_tag ā€¢ email_ļ¬eld_tag ROR Lab.
  • 16. Helpers Generating Form Elements ā€¢ check_box_tag ā€¢ radio_button_tag Modernizr ā€¢ text_area_tag yepnope ā€¢ password_ļ¬eld_tag ā€¢ hidden_ļ¬eld_tag polyļ¬ller ā€¢ search_ļ¬eld_tag ā€¢ telephone_ļ¬eld_tag HTML5 controls ā€¢ url_ļ¬eld_tag ā€¢ email_ļ¬eld_tag ROR Lab.
  • 17. HTML 5 Form Helpers Rails 3 HTML5 Input Types search_ļ¬eld search telephone_ļ¬eld tel url_ļ¬eld url email_ļ¬eld email number_ļ¬eld number range_ļ¬eld range Agile Web Development with Rails 4th edition ROR Lab.
  • 18. Model Object Helpers ROR Lab.
  • 19. Model Object Helpers ā€¢ check_box_tag ROR Lab.
  • 20. Model Object Helpers ā€¢ check_box_tag ā€¢ radio_button_tag ROR Lab.
  • 21. Model Object Helpers ā€¢ check_box_tag ā€¢ radio_button_tag ā€¢ text_area_tag ROR Lab.
  • 22. Model Object Helpers ā€¢ check_box_tag ā€¢ radio_button_tag ā€¢ text_area_tag ā€¢ password_ļ¬eld_tag ROR Lab.
  • 23. Model Object Helpers ā€¢ check_box_tag ā€¢ radio_button_tag ā€¢ text_area_tag ā€¢ password_ļ¬eld_tag ā€¢ hidden_ļ¬eld_tag ROR Lab.
  • 24. Model Object Helpers ā€¢ check_box_tag ā€¢ radio_button_tag ā€¢ No _tag ā€¢ text_area_tag ā€¢ password_ļ¬eld_tag ā€¢ hidden_ļ¬eld_tag ROR Lab.
  • 25. Model Object Helpers ā€¢ check_box_tag ā€¢ radio_button_tag ā€¢ No _tag ā€¢ text_area_tag ā€¢ password_ļ¬eld_tag ā€¢ hidden_ļ¬eld_tag ROR Lab.
  • 26. for Model Object @person -> name : ā€œHenryā€ <%= text_ļ¬eld(:person, :name) %> <input id="person_name" name="person[name]" type="text" ROR Lab.
  • 27. for Model Object @person -> name : ā€œHenryā€ <%= text_ļ¬eld(:person, :name) %> <input id="person_name" name="person[name]" type="text" ROR Lab.
  • 28. for Model Object @person -> name : ā€œHenryā€ <%= text_ļ¬eld(:person, :name) %> <input id="person_name" name="person[name]" type="text" ROR Lab.
  • 29. Binding a Form to an Object def new Ā Ā @article = Article.new end <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> ROR Lab.
  • 30. Binding a Form to an Object def new Ā Ā @article = Article.new end <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> ROR Lab.
  • 31. Binding a Form to an Object def new Ā Ā @article = Article.new end <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> ROR Lab.
  • 32. Binding a Form to an Object def new Ā Ā @article = Article.new end <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> ROR Lab.
  • 33. Binding a Form to an Object def new Ā Ā @article = Article.new end <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> ROR Lab.
  • 34. Binding a Form to an Object def new Ā Ā @article = Article.new end <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> ROR Lab.
  • 35. Binding a Form to an Object def new Ā Ā @article = Article.new end <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> ROR Lab.
  • 36. <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> <form accept-charset="UTF-8" action="/articles/create" method="post" class="nifty_form"> Ā Ā <input id="article_title" name="article[title]" size="30" type="text" /> Ā Ā <textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea> Ā Ā <input name="commit" type="submit" value="Create" /> </form> ROR Lab.
  • 37. <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> params[:article] <form accept-charset="UTF-8" action="/articles/create" method="post" class="nifty_form"> Ā Ā <input id="article_title" name="article[title]" size="30" params[:article][:title] type="text" /> Ā Ā <textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea> Ā Ā <input name="commit" type="submit" value="Create" /> </form>params[:article][:body] ROR Lab.
  • 38. <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> params[:article] <form accept-charset="UTF-8" action="/articles/create" method="post" class="nifty_form"> Ā Ā <input id="article_title" name="article[title]" size="30" params[:article][:title] type="text" /> Ā Ā <textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea> Ā Ā <input name="commit" type="submit" value="Create" /> </form>params[:article][:body] ROR Lab.
  • 39. <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> params[:article] <form accept-charset="UTF-8" action="/articles/create" method="post" class="nifty_form"> Ā Ā <input id="article_title" name="article[title]" size="30" params[:article][:title] type="text" /> Ā Ā <textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea> Ā Ā <input name="commit" type="submit" value="Create" /> </form>params[:article][:body] ROR Lab.
  • 40. <%= form_for @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %> Ā Ā <%= f.text_ļ¬eld :title %> Ā Ā <%= f.text_area :body, :size => "60x12" %> Ā Ā <%= f.submit "Create" %> <% end %> params[:article] <form accept-charset="UTF-8" action="/articles/create" method="post" class="nifty_form"> Ā Ā <input id="article_title" name="article[title]" size="30" params[:article][:title] type="text" /> Ā Ā <textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea> Ā Ā <input name="commit" type="submit" value="Create" /> </form>params[:article][:body] ROR Lab.
  • 41. ļ¬elds_for Dry Lab in Terminal ROR Lab.
  • 42. ļ¬elds_for Person ā€¢name ā€¢age ā€¢sex ā€¢address ā€¢telephone ROR Lab.
  • 43. ļ¬elds_for Person Contact ā€¢name ā€¢age ā€¢sex ā€¢address ā€¢telephone ROR Lab.
  • 44. ļ¬elds_for Person Contact ā€¢name ā€¢address ā€¢age ā€¢telephone ā€¢sex ROR Lab.
  • 45. ļ¬elds_for has_one Person Contact ā€¢name ā€¢address ā€¢age ā€¢telephone ā€¢sex ROR Lab.
  • 46. ļ¬elds_for has_one Person Contact ā€¢name ā€¢address ā€¢age belong_to ā€¢telephone ā€¢sex ROR Lab.
  • 47. ļ¬elds_for has_one Person Contact ā€¢name ā€¢address ā€¢age belong_to ā€¢telephone ā€¢sex class Person < ActiveRecord::Base has_one :contact, :dependent => :destroy end class Contact < ActiveRecord::Base belongs_to :person ROR Lab.
  • 48. Using Resources resources :articles in conļ¬g/routes.rb ## Creating a new article # long-style: form_for(@article, :url => articles_path) # same thing, short-style (record identiļ¬cation gets used): form_for(@article) Ā  ## Editing an existing article # long-style: form_for(@article, :url => article_path(@article), :html => { :method => "put" }) # short-style: form_for(@article) ROR Lab.
  • 49. Using Namespaces namespace :admin do in routes.rb Ā Ā resources :posts, :comments end form_for [:admin, @post] in _form.html.erb ROR Lab.
  • 50. HTTP Verbs ā€¢ GET for all browsers ā€¢ POST ā€¢ PUT ? ā€¢ DELETE ROR Lab.
  • 51. HTTP Verbs ā€¢ GET for all browsers ā€¢ POST ā€¢ PUT ? ā€¢ DELETE ROR Lab.
  • 52. HTTP Verbs ā€¢ GET for all browsers ā€¢ POST ā€¢ PUT ? ā€¢ DELETE ROR Lab.
  • 53. PUT & DELETE ā€¢ A hidden input : _method form_tag(search_path, :method => "put") <form accept-charset="UTF-8" action="/search" method="post"> Ā Ā <div style="margin:0;padding:0"> Ā Ā Ā Ā <input name="_method" type="hidden" value="put" /> Ā Ā Ā Ā <input name="utf8" type="hidden" value="&#x2713;" /> Ā Ā Ā Ā <input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" /> Ā Ā </div> Ā Ā ... ROR Lab.
  • 54. select_tag <select name="city_id" id="city_id"> Ā Ā <option value="1">Lisbon</option> Ā Ā <option value="2">Madrid</option> Ā Ā ... Ā Ā <option value="12">Berlin</option> </select> <%= select_tag(:city_id, '<option value="1">Lisbon</option>...') %> ROR Lab.
  • 55. options_for_select <%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...]) %> Ā  output: 1st argument => a nested array Ā  <option value="1">Lisbon</option> <option value="2">Madrid</option> ... <%= select_tag(:city_id, options_for_select(...)) %> ROR Lab.
  • 56. options_for_select <%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %> Ā  output: Ā  2nd argument => ā€˜selectedā€™ <option value="1">Lisbon</option> <option value="2" selected="selected">Madrid</option> ROR Lab.
  • 57. options_for_select <%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %> Ā  output: Ā  2nd argument => ā€˜selectedā€™ <option value="1">Lisbon</option> <option value="2" selected="selected">Madrid</option> ROR Lab.
  • 58. _tag select_tag # controller: @person = Person.new(:city_id => 2) # view: <%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %> ROR Lab.
  • 59. select_tag ORM for # controller: @person = Person.new(:city_id => 2) # view: <%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %> ROR Lab.
  • 60. select_tag ORM for # controller: @person = Person.new(:city_id => 2) # view: <%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %> 2 pre-selected by Rails ROR Lab.
  • 61. select_tag ORM for # controller: @person = Person.new(:city_id => 2) # view: <%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %> form builder 2 pre-selected by Rails # select on a form builder <%= f.select(:city_id, ...) %> ROR Lab.
  • 62. select from collection <% cities_array = City.all.map { |city| [city.name, city.id] } %> <%= options_for_select(cities_array) %> options _for_select ROR Lab.
  • 63. select from collection <% cities_array = City.all.map { |city| [city.name, city.id] } %> <%= options_for_select(cities_array) %> options _from_collection _for_select (City.all, :id, :name) ROR Lab.
  • 65. collection_select <%= collection_ collection_select <%= f.collection_select ROR Lab.
  • 66. collection_select <%= collection_ collection_select <%= f. f.collection_select ROR Lab.
  • 67. Special Select ā€¢ time_zone_select : time_zone_options_for_select ā€¢ country_select : isolated to country_select plugin ā€¢ select_date : barebones helper ā€¢ date_select : model object helper ROR Lab.
  • 68. select_date <%= select_date Date.today, :preļ¬x => :start_date %> <select id="start_date_year" name="start_date[year]"> ... </select> <select id="start_date_month" name="start_date[month]"> ... </select> <select id="start_date_day" name="start_date[day]"> ... </select> ROR Lab.
  • 69. select_date <%= select_date Date.today, :preļ¬x => :start_date %> <select id="start_date_year" name="start_date[year]"> ... </select> <select id="start_date_month" name="start_date[month]"> ... </select> <select id="start_date_day" name="start_date[day]"> ... </select> Date.civil(params[:start_date][:year].to_i, params[:start_date] [:month].to_i, params[:start_date][:day].to_i) ROR Lab.
  • 70. date_select <%= date_select :person, :birth_date %> <select id="person_birth_date_1i" name="person[birth_date(1i)]"> ... </select> <select id="person_birth_date_2i" name="person[birth_date(2i)]"> ... </select> <select id="person_birth_date_3i" ROR Lab.
  • 71. date_select <%= date_select :person, :birth_date %> <select id="person_birth_date_1i" name="person[birth_date(1i)]"> ... </select> <select id="person_birth_date_2i" name="person[birth_date(2i)]"> ... </select> <select id="person_birth_date_3i" {:person => {'birth_date(1i)' => '2008', 'birth_date(2i)' => '11', ROR Lab.
  • 72. Individual Selects ā€¢ select_year ā€¢ select_month ā€¢ select_day :preļ¬x defaults to ā€œdateā€ ā€¢ select_hour ā€¢ select_minute ā€¢ select_second ROR Lab.
  • 73. Individual Selects ā€¢ select_year ā€¢ select_month ā€¢ select_day :preļ¬x defaults to ā€œdateā€ ā€¢ select_hour ā€¢ select_minute ā€¢ select_second ROR Lab.
  • 74. Uploading Files params[:picture] <%= form_tag({:action => :upload}, :multipart => true) do %> Ā Ā <%= ļ¬le_ļ¬eld_tag 'picture' %> <% end %> Ā  <%= form_for @person , :multipart => true Ā Ā <%= f.ļ¬le_ļ¬eld :picture %> do |f| %> <% end %> params[:person][:picture] Since Rails 3.1, it automatically sets the multipart/ form-data with ļ¬le_ļ¬eld in the form_for ROR Lab.
  • 75. Uploading Files params[:picture] <%= form_tag({:action => :upload}, :multipart => true) do %> Ā Ā <%= ļ¬le_ļ¬eld_tag 'picture' %> <% end %> Ā  <%= form_for @person Ā Ā <%= f.ļ¬le_ļ¬eld :picture %> |f| %> do <% end %> params[:person][:picture] Since Rails 3.1, it automatically sets the multipart/ form-data with ļ¬le_ļ¬eld in the form_for ROR Lab.
  • 76. Uploading Files params[:picture] <%= form_tag({:action => :upload}, :multipart => true) do %> Ā Ā <%= ļ¬le_ļ¬eld_tag 'picture' %> <% end %> Ā  <%= form_for @person Ā Ā <%= f.ļ¬le_ļ¬eld :picture %> |f| %> do <% end %> params[:person][:picture] Since Rails 3.1, it automatically sets the multipart/ form-data with ļ¬le_ļ¬eld in the form_for ROR Lab.
  • 77. What gets uploaded def upload Ā Ā uploaded_io = params[:person][:picture] Ā Ā File.open(Rails.root.join('public', 'uploads', uploaded_io.original_ļ¬lename), 'w') do |ļ¬le| Ā Ā Ā Ā ļ¬le.write(uploaded_io.read) Ā Ā end ROR Lab.
  • 78. What gets uploaded def upload Ā Ā uploaded_io = params[:person][:picture] Ā Ā File.open(Rails.root.join('public', 'uploads', uploaded_io.original_ļ¬lename), 'w') do |ļ¬le| Ā Ā Ā Ā ļ¬le.write(uploaded_io.read) Ā Ā end an instance of a subclass of IO ā€¢ original_ļ¬lename ā€¢ content_type : MIME type ROR Lab.
  • 80. Ajaxing Upload ā€¢ ā€œremotipartā€ gem : AJAX style ļ¬le uploads with jQuery https://github.com/leppert/remotipart ROR Lab.
  • 81. Customizing Form Builders ROR Lab.
  • 82. Customizing Form Builders <%= form_for @person do |f| %> Ā Ā <%= text_ļ¬eld_with_label f, :ļ¬rst_name %> ROR Lab.
  • 83. Customizing Form Builders <%= form_for @person do |f| %> Ā Ā <%= text_ļ¬eld_with_label f, :ļ¬rst_name %> <%= form_for @person, :builder => LabellingFormBuilder do |f| %> Ā Ā <%= f.text_ļ¬eld :ļ¬rst_name %> class LabellingFormBuilder < ActionView::Helpers::FormBuilder Ā Ā def text_ļ¬eld(attribute, options={}) Ā Ā Ā Ā label(attribute) + super Ā Ā end end ROR Lab.
  • 85. Params Naming Client Server Form for model obj ā€œ@personā€ params[:person] submit ROR Lab.
  • 86. Params Naming Client Server Form for model obj ā€œ@personā€ params[:person] submit ROR Lab.
  • 87. Basic Structures Arrays & Hashes <input id="person_name" name="person[name]" type="text" value="Henry"/> params hash {ā€˜personā€™ => {ā€˜nameā€™ => ā€˜Henryā€™}} params[:person] {ā€˜nameā€™ => ā€˜Henryā€™} params[:person][:name] ā€˜Henryā€™ ROR Lab.
  • 88. Nested Hashes <input id="person_address_city" name="person[address][city]" type="text" value="New York"/> params hash {'person' => {'address' => {'city' => 'New York'}}} ROR Lab.
  • 89. Duplicated Params <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> params[:person][:phone_number] [ ā€™02-333-1234ā€™, ā€˜031-323-9898ā€™, ā€˜062-546-2323ā€™ ] ROR Lab.
  • 90. Duplicated Params <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> params[:person][:phone_number] [ ā€™02-333-1234ā€™, ā€˜031-323-9898ā€™, ā€˜062-546-2323ā€™ ] ROR Lab.
  • 91. Duplicated Params <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> params[:person][:phone_number] [ ā€™02-333-1234ā€™, ā€˜031-323-9898ā€™, ā€˜062-546-2323ā€™ ] ROR Lab.
  • 92. Duplicated Params <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> <input name="person[phone_number][]" type="text"/> params[:person][:phone_number] [ ā€™02-333-1234ā€™, ā€˜031-323-9898ā€™, ā€˜062-546-2323ā€™ ] ROR Lab.
  • 93. Mixed Params hash nth-nested but array only one-level <input name="addresses[][line1]" type="text"/> <input name="addresses[][line2]" type="text"/> <input name="addresses[][city]" type="text"/> params[:addresses] [ { ā€˜line1ā€™ => ā€™02-333-1234ā€™, ā€˜line2ā€™ => ā€˜031-323-9898ā€™, ā€˜cityā€™ => ā€˜seoulā€™ } ] ROR Lab.
  • 94. Using Form Helpers <%= form_for @person do |person_form| %> Ā Ā <%= person_form.text_ļ¬eld :name %> Ā Ā <% @person.addresses.each do |address| %> Ā Ā Ā Ā <%= person_form.ļ¬elds_for address, :index => address do | address_form|%> Ā Ā Ā Ā Ā Ā <%= address_form.text_ļ¬eld :city %> Ā Ā Ā Ā <% end %> Ā Ā <% end %> <% end %> <form accept-charset="UTF-8" action="/people/1" class="edit_person" id="edit_person_1" method="post"> Ā Ā <input id="person_name" name="person[name]" size="30" type="text" /> Ā Ā <input id="person_address_23_city" name="person[address][23][city]" size="30" type="text" /> Ā Ā <input id="person_address_45_city" name="person[address][45][city]" size="30" type="text" /> </form> ROR Lab.
  • 95. Using Form Helpers { 'person' => { 'name' => 'Bob', 'address' => { '23' => {'city' => 'Paris'}, '45' => {'city' => 'London'} } } ROR Lab.
  • 96. Using :index <%= ļ¬elds_for 'person[address][primary]', address, :index => address do |address_form| %> Ā Ā <%= address_form.text_ļ¬eld :city %> <%= ļ¬elds_for 'person[address][primary][]', address do | address_form| %> Ā Ā <%= address_form.text_ļ¬eld :city %> ROR Lab.
  • 97. Form to External Resources - 1 <%= form_tag 'http://farfar.away/form', :authenticity_token => 'external_token') do %> Ā Ā Form contents <%= form_tag 'http://farfar.away/form', :authenticity_token => false) do %> Ā Ā Form contents ROR Lab.
  • 98. Form to External Resources - 1 <%= form_tag 'http://farfar.away/form', :authenticity_token => 'external_token') do %> Ā Ā Form contents payment gateway <%= form_tag 'http://farfar.away/form', :authenticity_token => false) do %> Ā Ā Form contents ROR Lab.
  • 99. Form to External Resources - 1 <%= form_tag 'http://farfar.away/form', :authenticity_token => 'external_token') do %> Ā Ā Form contents <%= form_tag 'http://farfar.away/form', :authenticity_token => false) do %> Ā Ā Form contents payment gateway ROR Lab.
  • 100. Form to External Resources - 2 <%= form_for @invoice, :url => external_url, :authenticity_token => 'external_token' do |f| Ā Ā Form contents <% end %> <%= form_for @invoice, :url => external_url, :authenticity_token => false do |f| Ā Ā Form contents ROR Lab.
  • 101. Form to External Resources - 2 <%= form_for @invoice, :url => form_for external_url, :authenticity_token => 'external_token' do |f| Ā Ā Form contents <% end %> <%= form_for @invoice, :url => form_for external_url, :authenticity_token => false do |f| Ā Ā Form contents ROR Lab.
  • 102. Complex forms Railscasts by Ryan Bates ROR Lab.
  • 103. Complex forms Railscasts by Ryan Bates ā€¢ Complex Forms Part 1 - Episode #73 ā€¢ Complex Forms Part II - Episode #74 ā€¢ Complex Forms Part III - Episode #75 ā€¢ Nested Model Form (resivsed) - Episode #196 ROR Lab.
  • 105. Ā  ROR Lab.

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. \n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. \n
  97. \n
  98. \n
  99. \n
  100. \n
  101. \n
  102. \n
  103. \n
  104. \n
  105. \n
  106. \n
  107. \n
  108. \n
  109. \n
  110. \n
  111. \n
  112. \n
  113. \n
  114. \n
  115. \n
  116. \n
  117. \n
  118. \n
  119. \n
  120. \n
  121. \n
  122. \n
  123. \n
  124. \n
  125. \n
  126. \n
  127. \n
  128. \n
  129. \n
  130. \n
  131. \n
  132. \n
  133. \n
  134. \n
  135. \n
  136. \n
  137. \n
  138. \n
  139. \n
  140. \n
  141. \n
  142. \n
  143. \n
  144. \n
  145. \n
  146. \n
  147. \n
  148. \n
  149. \n
  150. \n
  151. \n
  152. \n
  153. \n
  154. \n
  155. \n