• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Action View Form Helpers - 1, Season 2
 

Action View Form Helpers - 1, Season 2

on

  • 870 views

 

Statistics

Views

Total Views
870
Views on SlideShare
707
Embed Views
163

Actions

Likes
0
Downloads
8
Comments
0

3 Embeds 163

http://rorlab.org 114
http://lab.wordstory.net 48
https://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Action View Form Helpers - 1, Season 2 Action View Form Helpers - 1, Season 2 Presentation Transcript

    • Ror lab. season 2 - the 13th round - Action ViewForm Helpers - 1 December 29th, 2012 Hyoseong Choi
    • form_tag• Two arguments 1. path for action : absolutely 1st argument 2. options hash • HTTP method : post(default), get, put, delete • HTML options : class, style, ...
    • form_tag Basic Form<%= form_tag do %>  Form contents<% end %> app/views/home/index.html.erb current action<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 to prevent</form> CSRF
    • form_tag Multiple Hashesform_tag(:controller => "people", :action =>"search", :method => "get", :class => "nifty_form")# => <form accept-charset="UTF-8" action="/people/search?method=get&class=nifty_form" method="post">form_tag({:controller => "people", :action =>"search"}, :method => "get", :class => "nifty_form")# => <form accept-charset="UTF-8" action="/people/search" method="get" class="nifty_form">
    • Helpers Generating Form Elements• text_field_tag• text_area_tag http://modernizr.com/docs/• check_box_tag Modernizr• radio_button_tag yepnope• password_field_tag• hidden_field_tag polyfiller• search_field_tag• telephone_field_tag HTML5 controls• url_field_tag• email_field_taghttp://en.wikipedia.org/wiki/Polyfillhttps://github.com/russfrisch/modernizr-rails
    • http://html5readiness.com
    • HTML 5 “Form 2.0”Firefox v17.0.1 Safari v6.0.2Chrome v23.0.1271.97 Opera v12.11
    • HTML 5 “Form 2.0” Form Element Helpers Rails 3 HTML5 Input Types search_field search telephone_field tel url_field url email_field email number_field number range_field range Agile Web Development with Rails 4th edition
    • HTML 5 “Form 2.0”Firefox v17.0.1 Safari v6.0.2Chrome v23.0.1271.97 Opera v12.11
    • Search Form for bookmark<%= form_tag("/search", :method => "get") do %>  <%= label_tag(:q, "Search for:") %>  <%= text_field_tag(:q) %>  <%= submit_tag("Search") %><% end %><form accept-charset="UTF-8" action="/search"method="get">  <label for="q">Search for:</label>  <input id="q" name="q" type="text" />  <input name="commit" type="submit" value="Search" /></form>
    • Form Elements Helpers Checkboxes <%= check_box_tag(:pet_dog) %> <%= label_tag(:pet_dog, "I own a dog") %> <%= check_box_tag(:pet_cat) %> <%= label_tag(:pet_cat, "I own a cat") %> <input id="pet_dog" name="pet_dog" type="checkbox" value="1" /> <label for="pet_dog">I own a dog</label> <input id="pet_cat" name="pet_cat" type="checkbox" value="1" /> <label for="pet_cat">I own a cat</label>
    • Form Elements Helpers Radio Buttons <%= radio_button_tag(:age, "child") %> <%= label_tag(:age_child, "I am younger than 21") %> <%= radio_button_tag(:age, "adult") %> <%= label_tag(:age_adult, "Im over 21") %> <input id="age_child" name="age" type="radio" value="child" /> <label for="age_child">I am younger than 21</label> <input id="age_adult" name="age" type="radio" value="adult" /> <label for="age_adult">Im over 21</label>
    • Form Elements Helpers Others <input id="age_child" name="age" type="radio" value="child" /> <label for="age_child">I am younger than 21</label> <input id="age_adult" name="age" type="radio" value="adult" /> <label for="age_adult">Im over 21</label> <input id="age_child" name="age" type="radio" value="child" /> <label for="age_child">I am younger than 21</label> <input id="age_adult" name="age" type="radio" value="adult" /> <label for="age_adult">Im over 21</label>
    • Model Object Helpers • check_box_tag • radio_button_tag• No _tag • text_area_tag • password_field_tag • hidden_field_tag
    • for Model Object @person -> name : “Henry”<%= text_field(:person, :name) %> for an instance for a method variable name(attr.)<input id="person_name" name="person[name]"type="text" value="Henry"/>
    • Binding a Form to an Objectdef new  @article = Article.newend<%= form_for @article, :url => { :action =>"create" }, :html => {:class => "nifty_form"} do |f|%>  <%= f.text_field :title %>  <%= f.text_area :body, :size => "60x12" %>  <%= f.submit "Create" %><% end %>
    • <%= form_for @article, :url => { :action =>"create" }, :html => {:class => "nifty_form"} do |f|%>  <%= f.text_field :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>
    • in the controller,params[:article]params[:article][:title]params[:article][:body]
    • fields_forPerson •name •age •sex •address •telephone
    • fields_for has_one orPerson Contact has_many •name •address •age •telephone •sex belong_to using nested attributes
    • fields_forclass Person < ActiveRecord::Base has_one :contact, :dependent => :destroyendclass Contact < ActiveRecord::Base belongs_to :personend using accepts_nested_attributes_for
    • Using Resources resources :articles in config/routes.rb## Creating a new article# long-style:form_for(@article, :url => articles_path)# short-style (record identification 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)
    • Using Namespacesnamespace :admin do  resources :posts, :commentsend in routes.rbform_for [:admin, @post] in _form.html.erbadmin_post_path(@post) on updateapp/controllers/admin/posts_controller.rbapp/controllers/admin/comments_controller.rbapp/views/admin/posts/new.html.erbapp/views/admin/posts/edit.html.erb
    • HTTP Verbs• GET for all browsers• POST• PUT ?• DELETE
    • PUT & DELETE • A hidden input : _methodform_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>  ...
    • 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>...) %>
    • options_for_select <%= options_for_select([[Lisbon, 1], [Madrid, 2], ...]) %>   1st argument => a nested array output:   <option value="1">Lisbon</option> <option value="2">Madrid</option> ... <%= select_tag(:city_id, options_for_select(...)) %>
    • 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>
    • select for ORM # controller: @person = Person.new(:city_id => 2) # view: <%= select(:person, :city_id, [[Lisbon, 1], [Madrid, 2], ...]) %>form builder pre-selected by Rails # select on a form builder <%= f.select(:city_id, ...) %>
    • select from collection <% cities_array = City.all.map { |city| [city.name, city.id] } %> <%= options_for_select(cities_array) %>in short form, options_from_collection (City.all, :id, :name)
    • collection_select collection_<%= collection_select (:person, :city_id, City.all, :id, :name) %> or f.<%= f.collection_select (:city_id, City.all, :id, :name) %>
    • 감사합니다.
    •    ROR Lab.