Action View Form Helpers - 1, Season 2

32
Action View Form Helpers - 1 Ror lab. season 2 - the 13th round - December 29th, 2012 Hyoseong Choi

description

 

Transcript of Action View Form Helpers - 1, Season 2

Page 1: Action View Form Helpers - 1, Season 2

Action View Form Helpers - 1

Ror lab. season 2- the 13th round -

December 29th, 2012

Hyoseong Choi

Page 2: Action View Form Helpers - 1, Season 2

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, ...

Page 3: Action View Form Helpers - 1, Season 2

form_tag

<%= 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>

Basic Form

app/views/home/index.html.erb

current action

to prevent CSRF

Page 4: Action View Form Helpers - 1, Season 2

form_tag

form_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">'

Multiple Hashes

Page 5: Action View Form Helpers - 1, Season 2

Helpers

• text_field_tag

• text_area_tag

• check_box_tag

• radio_button_tag

• password_field_tag

• hidden_field_tag

• search_field_tag

• telephone_field_tag

• url_field_tag

• email_field_tag

Generating Form Elements

HTML5 controls

Modernizryepnope

polyfiller

http://en.wikipedia.org/wiki/Polyfillhttps://github.com/russfrisch/modernizr-rails

http://modernizr.com/docs/

Page 6: Action View Form Helpers - 1, Season 2

http://html5readiness.com

Page 7: Action View Form Helpers - 1, Season 2

Opera v12.11

Safari v6.0.2

Chrome v23.0.1271.97

Firefox v17.0.1

HTML 5 “Form 2.0”

Page 8: Action View Form Helpers - 1, Season 2

Rails 3 HTML5 Input Types

search_field search

telephone_field tel

url_field url

email_field email

number_field number

range_field range

Form Element Helpers

Agile Web Development with Rails 4th edition

HTML 5 “Form 2.0”

Page 9: Action View Form Helpers - 1, Season 2

Opera v12.11

Safari v6.0.2

Chrome v23.0.1271.97

Firefox v17.0.1

HTML 5 “Form 2.0”

Page 10: Action View Form Helpers - 1, Season 2

Search Form

<%= form_tag("/search", :method => "get") do %>  <%= label_tag(:q, "Search for:") %>  <%= text_field_tag(:q) %>  <%= submit_tag("Search") %><% end %>

for bookmark

<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>

Page 11: Action View Form Helpers - 1, Season 2

Form Elements Helpers

<%= 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>

Checkboxes

Page 12: Action View Form Helpers - 1, Season 2

Form Elements Helpers

<%= radio_button_tag(:age, "child") %><%= label_tag(:age_child, "I am younger than 21") %><%= radio_button_tag(:age, "adult") %><%= label_tag(:age_adult, "I'm 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">I'm over 21</label>

Radio Buttons

Page 13: Action View Form Helpers - 1, Season 2

Form Elements Helpers

<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">I'm 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">I'm over 21</label>

Others

Page 14: Action View Form Helpers - 1, Season 2

Model Object Helpers

• No _tag

• check_box_tag

• radio_button_tag

• text_area_tag

• password_field_tag

• hidden_field_tag

Page 15: Action View Form Helpers - 1, Season 2

for Model Object

<%= text_field(:person, :name) %>

<input id="person_name" name="person[name]" type="text" value="Henry"/>

@person -> name : “Henry”

for an instance variable

for a method name(attr.)

Page 16: Action View Form Helpers - 1, Season 2

Binding a Form to an Object

def 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 %>

Page 17: Action View Form Helpers - 1, Season 2

<%= 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>

Page 18: Action View Form Helpers - 1, Season 2

params[:article]

params[:article][:title]

params[:article][:body]

in the controller,

Page 19: Action View Form Helpers - 1, Season 2

fields_for

•name•age•sex

Person

•address•telephone

Page 20: Action View Form Helpers - 1, Season 2

fields_for

•name•age•sex

Person

•address•telephone

Contact

has_oneor

has_many

belong_to

using nested attributes

Page 21: Action View Form Helpers - 1, Season 2

fields_for

class Person < ActiveRecord::Base has_one :contact, :dependent => :destroyend

class Contact < ActiveRecord::Base belongs_to :personend

using accepts_nested_attributes_for

Page 22: Action View Form Helpers - 1, Season 2

Using Resources

## Creating a new article# long-style:form_for(@article, :url => articles_path)# short-style (record identification gets used):form_for(@article)

resources :articles in config/routes.rb

## Editing an existing article# long-style:form_for(@article, :url => article_path(@article), :html => { :method => "put" })# short-style:form_for(@article)

Page 23: Action View Form Helpers - 1, Season 2

Using Namespaces

namespace :admin do  resources :posts, :commentsend

form_for [:admin, @post]

in routes.rb

in _form.html.erb

app/controllers/admin/posts_controller.rbapp/controllers/admin/comments_controller.rbapp/views/admin/posts/new.html.erbapp/views/admin/posts/edit.html.erb

admin_post_path(@post) on update

Page 24: Action View Form Helpers - 1, Season 2

HTTP Verbs

• GET

• POST

• PUT

• DELETE?

for all browsers

Page 25: Action View Form Helpers - 1, Season 2

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>  ...

Page 26: Action View Form Helpers - 1, Season 2

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>...') %>

Page 27: Action View Form Helpers - 1, Season 2

options_for_select

<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...]) %> output: <option value="1">Lisbon</option><option value="2">Madrid</option>...

1st argument => a nested array

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

Page 28: Action View Form Helpers - 1, Season 2

options_for_select

<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %> output: <option value="1">Lisbon</option><option value="2" selected="selected">Madrid</option>

2nd argument => ‘selected’

Page 29: Action View Form Helpers - 1, Season 2

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

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

pre-selected by Rails

# select on a form builder<%= f.select(:city_id, ...) %>

form

bui

lder

Page 30: Action View Form Helpers - 1, Season 2

select from collection

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

options_from_collection

(City.all, :id, :name)

in short form,

Page 31: Action View Form Helpers - 1, Season 2

collection_select

<%= collection_select (:person, :city_id, City.all, :id, :name) %>

collection_

<%= f.collection_select (:city_id, City.all, :id, :name) %>

f.

or

Page 32: Action View Form Helpers - 1, Season 2

ROR Lab.

감사합니다.����������� ������������������