SlideShare a Scribd company logo
Rails View – Chapter 5: Form
• Form Helper
• Simple Form (gem)
• Nested Form (gem)
• Custom Form
Form Helper
form_for:
 we use it for smart model object.


form_tag:
 we use it for non-smart model object. (search
form…)
Form Helper
= form_for @creation, html: {multipart: true} do |f|
 = f.label :name
 = f.text_field :name
 = f.label :description
 = f.text_area :description, cols: 40, rows: 4


 = f.file_field :file
 = f.label :file


 = f.submit
Simple Form
Add it to your Gemfile:
 gem 'simple_form'
Run the following command to install it:
 bundle install
Run the generator:
 rails generate simple_form:install
Run the generator with bootstrap:
 rails generate simple_form:install --bootstrap
Simple Form
= simple_form_for @user do |f|
 = f.input :username
 = f.input :password
 = f.button :submit
Simple Form
simple_form_for @user do |f|
 = f.input :username, label: “Your username please”
 = f.input :password, hint: “No special characters.”
 = f.input :email, placeholder: “user@domain.com”
 = f.input :remember_me, inline_label: “Yes,
remember me”
 = f.button :submit
Simple Form
input_html:
 pass any html attribute straight to the input.
label_html:
 pass any html attribute straight to the label.
wrapper_html:
 pass any html attribute straight to the div wrapper.
Simple Form
= simple_form_for @user do |f|
 = f.input :username, label_html: { class: “special” }
 = f.input :password, input_html: { maxlength: 20 }
 = f.input :remember_me, wrapper_html: { id:
“wrapper” }
 = f.button :submit
Simple Form
= simple_form_for @user, defaults: { input_html:
{ class: 'default_class' } } do |f|
 = f.input :name, required: false
 = f.input :description, as: :text
 = f.input :accepts, as: :radio_buttons
Simple Form
= f.input :price, wrapper: :prepend do
 = content_tag :span, “USD”, class: “add-on”
 = f.input_field :price


## Output
<div class=“input-prepend”>
 <span class=“add-on”>USD</span>
 <input class=“numeric decimal required” id=“food_price”
name=“food[price]” type=“text”>
Simple Form
user belongs_to company, company has_many users
user has_many roles, role belongs_to user


= simple_form_for @user do |f|
 = f.input :name
 = f.association :company
 = f.association :roles
 = f.button :submit
Simple Form
as: :radio_buttons
as: :check_boxes
collection: Company.active.all
prompt: "Choose a Company“
include_blank: false
label_method: :name
value_method: :uuid
Simple Form
= simple_form_for @user do |f|
 = f.input :name
 = f.simple_field_for :image do |img|
  = img.input :photo, as: :file
Nested Form
This gem is applied to nested form.
• form_for => nested_form_for
• simple_form_for => simple_nested_form_for
Nested Form
= simple_form_for @user do |f|
 = f.input :name
 = f.simple_field_for :images do |img|
  = img.input :photo, as: :file
  = img.link_to_remove “remove this image”
 %p
  f.link_to_add “add image”, :images
Custom Form
## artflow/forms/lib/application_form_builder.rb
class ApplicationFormBuilder <
ActionView::Helpers::FormBuilder
End




## artflow/forms/config/application.rb
config.autoload_paths += %W(#{config.root}/lib)
Custom Form
= f.field_item :name do
 = f.text_field :name


## artflow/forms/lib/application_form_builder.rb
def field_item(attribute, text = nil, &block)
 @template.content_tag :li do
  @template.concat @template.label(attribute, text)
  yield
 end
end
Custom Form
Hook It Up


= form_for @creation, builder: ApplicationFormBuilder,
html: {multipart: true} do |f|




## artflow/forms/config/initializers/form_builder_initializer.rb
ActionView::Base.default_form_builder = ApplicationFormBuilder
Reference
- simple_form: (https://github.com/plataformatec/simple_form)
- nested_form: (https://github.com/ryanb/nested_form)
The End

More Related Content

What's hot

Oop php 5
Oop php 5Oop php 5
Oop php 5
phpubl
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
Ravi Bhadauria
 
Zend framework 04 - forms
Zend framework 04 - formsZend framework 04 - forms
Zend framework 04 - forms
Tricode (part of Dept)
 
Custom Signals for Uncoupled Design
Custom Signals for Uncoupled DesignCustom Signals for Uncoupled Design
Custom Signals for Uncoupled Design
ecomsmith
 
Forms, Getting Your Money's Worth
Forms, Getting Your Money's WorthForms, Getting Your Money's Worth
Forms, Getting Your Money's Worth
Alex Gaynor
 
Advanced Custom Fields - Flexible Content
Advanced Custom Fields - Flexible ContentAdvanced Custom Fields - Flexible Content
Advanced Custom Fields - Flexible Content
Norm Euker
 
Components are the Future of the Web: It’s Going To Be Okay
Components are the Future of the Web: It’s Going To Be OkayComponents are the Future of the Web: It’s Going To Be Okay
Components are the Future of the Web: It’s Going To Be Okay
FITC
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
Naveen Kumar Veligeti
 
Form Validation
Form ValidationForm Validation
Form Validation
Graeme Smith
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
Morshedul Arefin
 
HTML5 Forms - KISS time - Fronteers
HTML5 Forms - KISS time - FronteersHTML5 Forms - KISS time - Fronteers
HTML5 Forms - KISS time - Fronteers
Robert Nyman
 
In some simple steps, your site can stand out from the rest. Here's how...
In some simple steps, your site can stand out from the rest. Here's how... In some simple steps, your site can stand out from the rest. Here's how...
In some simple steps, your site can stand out from the rest. Here's how...
British Council
 

What's hot (12)

Oop php 5
Oop php 5Oop php 5
Oop php 5
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
 
Zend framework 04 - forms
Zend framework 04 - formsZend framework 04 - forms
Zend framework 04 - forms
 
Custom Signals for Uncoupled Design
Custom Signals for Uncoupled DesignCustom Signals for Uncoupled Design
Custom Signals for Uncoupled Design
 
Forms, Getting Your Money's Worth
Forms, Getting Your Money's WorthForms, Getting Your Money's Worth
Forms, Getting Your Money's Worth
 
Advanced Custom Fields - Flexible Content
Advanced Custom Fields - Flexible ContentAdvanced Custom Fields - Flexible Content
Advanced Custom Fields - Flexible Content
 
Components are the Future of the Web: It’s Going To Be Okay
Components are the Future of the Web: It’s Going To Be OkayComponents are the Future of the Web: It’s Going To Be Okay
Components are the Future of the Web: It’s Going To Be Okay
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
 
Form Validation
Form ValidationForm Validation
Form Validation
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
 
HTML5 Forms - KISS time - Fronteers
HTML5 Forms - KISS time - FronteersHTML5 Forms - KISS time - Fronteers
HTML5 Forms - KISS time - Fronteers
 
In some simple steps, your site can stand out from the rest. Here's how...
In some simple steps, your site can stand out from the rest. Here's how... In some simple steps, your site can stand out from the rest. Here's how...
In some simple steps, your site can stand out from the rest. Here's how...
 

Similar to Rails view chapte 5 - form

A multi submission importer for easyform
A multi submission importer for easyformA multi submission importer for easyform
A multi submission importer for easyform
Annette Lewis
 
Creating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in Python
Siddhi
 
Curso Symfony - Clase 4
Curso Symfony - Clase 4Curso Symfony - Clase 4
Curso Symfony - Clase 4
Javier Eguiluz
 
Facebook Development with Zend Framework
Facebook Development with Zend FrameworkFacebook Development with Zend Framework
Facebook Development with Zend Framework
Brett Harris
 
Django Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, TricksDjango Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, Tricks
Shawn Rider
 
Working With The Symfony Admin Generator
Working With The Symfony Admin GeneratorWorking With The Symfony Admin Generator
Working With The Symfony Admin Generator
John Cleveley
 
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
RORLAB
 
Session6
Session6Session6
The Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsThe Django Book - Chapter 7 forms
The Django Book - Chapter 7 forms
Vincent Chien
 
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
RORLAB
 
Symfony Form Basics - OroMeetup #3 Cherkassy
Symfony Form Basics - OroMeetup #3 CherkassySymfony Form Basics - OroMeetup #3 Cherkassy
Symfony Form Basics - OroMeetup #3 Cherkassy
Andrew Yatsenko
 
WordPress as a Content Management System
WordPress as a Content Management SystemWordPress as a Content Management System
WordPress as a Content Management System
Valent Mustamin
 
HTML::FormHandler
HTML::FormHandlerHTML::FormHandler
HTML::FormHandler
bbeeley
 
Fixture Replacement Plugins
Fixture Replacement PluginsFixture Replacement Plugins
Fixture Replacement Plugins
Paul Klipp
 
The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010
Fabien Potencier
 
Everything you always wanted to know about forms* *but were afraid to ask
Everything you always wanted to know about forms* *but were afraid to askEverything you always wanted to know about forms* *but were afraid to ask
Everything you always wanted to know about forms* *but were afraid to ask
Andrea Giuliano
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
Fabien Potencier
 
Migrare da symfony 1 a Symfony2
 Migrare da symfony 1 a Symfony2  Migrare da symfony 1 a Symfony2
Migrare da symfony 1 a Symfony2
Massimiliano Arione
 
Symfony2. Form and Validation
Symfony2. Form and ValidationSymfony2. Form and Validation
Symfony2. Form and Validation
Vladimir Doroshenko
 
Web осень 2012 лекция 7
Web осень 2012 лекция 7Web осень 2012 лекция 7
Web осень 2012 лекция 7
Technopark
 

Similar to Rails view chapte 5 - form (20)

A multi submission importer for easyform
A multi submission importer for easyformA multi submission importer for easyform
A multi submission importer for easyform
 
Creating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in Python
 
Curso Symfony - Clase 4
Curso Symfony - Clase 4Curso Symfony - Clase 4
Curso Symfony - Clase 4
 
Facebook Development with Zend Framework
Facebook Development with Zend FrameworkFacebook Development with Zend Framework
Facebook Development with Zend Framework
 
Django Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, TricksDjango Forms: Best Practices, Tips, Tricks
Django Forms: Best Practices, Tips, Tricks
 
Working With The Symfony Admin Generator
Working With The Symfony Admin GeneratorWorking With The Symfony Admin Generator
Working With The Symfony Admin Generator
 
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
 
Session6
Session6Session6
Session6
 
The Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsThe Django Book - Chapter 7 forms
The Django Book - Chapter 7 forms
 
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
 
Symfony Form Basics - OroMeetup #3 Cherkassy
Symfony Form Basics - OroMeetup #3 CherkassySymfony Form Basics - OroMeetup #3 Cherkassy
Symfony Form Basics - OroMeetup #3 Cherkassy
 
WordPress as a Content Management System
WordPress as a Content Management SystemWordPress as a Content Management System
WordPress as a Content Management System
 
HTML::FormHandler
HTML::FormHandlerHTML::FormHandler
HTML::FormHandler
 
Fixture Replacement Plugins
Fixture Replacement PluginsFixture Replacement Plugins
Fixture Replacement Plugins
 
The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010The state of Symfony2 - SymfonyDay 2010
The state of Symfony2 - SymfonyDay 2010
 
Everything you always wanted to know about forms* *but were afraid to ask
Everything you always wanted to know about forms* *but were afraid to askEverything you always wanted to know about forms* *but were afraid to ask
Everything you always wanted to know about forms* *but were afraid to ask
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
 
Migrare da symfony 1 a Symfony2
 Migrare da symfony 1 a Symfony2  Migrare da symfony 1 a Symfony2
Migrare da symfony 1 a Symfony2
 
Symfony2. Form and Validation
Symfony2. Form and ValidationSymfony2. Form and Validation
Symfony2. Form and Validation
 
Web осень 2012 лекция 7
Web осень 2012 лекция 7Web осень 2012 лекция 7
Web осень 2012 лекция 7
 

Rails view chapte 5 - form

  • 1. Rails View – Chapter 5: Form • Form Helper • Simple Form (gem) • Nested Form (gem) • Custom Form
  • 2. Form Helper form_for: we use it for smart model object. form_tag: we use it for non-smart model object. (search form…)
  • 3. Form Helper = form_for @creation, html: {multipart: true} do |f| = f.label :name = f.text_field :name = f.label :description = f.text_area :description, cols: 40, rows: 4 = f.file_field :file = f.label :file = f.submit
  • 4. Simple Form Add it to your Gemfile: gem 'simple_form' Run the following command to install it: bundle install Run the generator: rails generate simple_form:install Run the generator with bootstrap: rails generate simple_form:install --bootstrap
  • 5. Simple Form = simple_form_for @user do |f| = f.input :username = f.input :password = f.button :submit
  • 6. Simple Form simple_form_for @user do |f| = f.input :username, label: “Your username please” = f.input :password, hint: “No special characters.” = f.input :email, placeholder: “user@domain.com” = f.input :remember_me, inline_label: “Yes, remember me” = f.button :submit
  • 7. Simple Form input_html: pass any html attribute straight to the input. label_html: pass any html attribute straight to the label. wrapper_html: pass any html attribute straight to the div wrapper.
  • 8. Simple Form = simple_form_for @user do |f| = f.input :username, label_html: { class: “special” } = f.input :password, input_html: { maxlength: 20 } = f.input :remember_me, wrapper_html: { id: “wrapper” } = f.button :submit
  • 9. Simple Form = simple_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f| = f.input :name, required: false = f.input :description, as: :text = f.input :accepts, as: :radio_buttons
  • 10. Simple Form = f.input :price, wrapper: :prepend do = content_tag :span, “USD”, class: “add-on” = f.input_field :price ## Output <div class=“input-prepend”> <span class=“add-on”>USD</span> <input class=“numeric decimal required” id=“food_price” name=“food[price]” type=“text”>
  • 11. Simple Form user belongs_to company, company has_many users user has_many roles, role belongs_to user = simple_form_for @user do |f| = f.input :name = f.association :company = f.association :roles = f.button :submit
  • 12. Simple Form as: :radio_buttons as: :check_boxes collection: Company.active.all prompt: "Choose a Company“ include_blank: false label_method: :name value_method: :uuid
  • 13. Simple Form = simple_form_for @user do |f| = f.input :name = f.simple_field_for :image do |img| = img.input :photo, as: :file
  • 14. Nested Form This gem is applied to nested form. • form_for => nested_form_for • simple_form_for => simple_nested_form_for
  • 15. Nested Form = simple_form_for @user do |f| = f.input :name = f.simple_field_for :images do |img| = img.input :photo, as: :file = img.link_to_remove “remove this image” %p f.link_to_add “add image”, :images
  • 16. Custom Form ## artflow/forms/lib/application_form_builder.rb class ApplicationFormBuilder < ActionView::Helpers::FormBuilder End ## artflow/forms/config/application.rb config.autoload_paths += %W(#{config.root}/lib)
  • 17. Custom Form = f.field_item :name do = f.text_field :name ## artflow/forms/lib/application_form_builder.rb def field_item(attribute, text = nil, &block) @template.content_tag :li do @template.concat @template.label(attribute, text) yield end end
  • 18. Custom Form Hook It Up = form_for @creation, builder: ApplicationFormBuilder, html: {multipart: true} do |f| ## artflow/forms/config/initializers/form_builder_initializer.rb ActionView::Base.default_form_builder = ApplicationFormBuilder
  • 19. Reference - simple_form: (https://github.com/plataformatec/simple_form) - nested_form: (https://github.com/ryanb/nested_form)