This document discusses Rails form helpers and generating forms in Rails. It covers the form_tag helper which generates form tags, and the form_for helper which binds forms to model objects. It also discusses generating individual form fields like text fields, text areas, checkboxes and radio buttons. Additionally, it covers options for selecting values from a collection and working with nested attributes using fields_for.
7. HTML 5 “Form 2.0”
Firefox v17.0.1 Safari v6.0.2
Chrome v23.0.1271.97 Opera v12.11
8. 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
9. HTML 5 “Form 2.0”
Firefox v17.0.1 Safari v6.0.2
Chrome v23.0.1271.97 Opera v12.11
11. 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>
12. 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, "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>
13. 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">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>
14. Model Object
Helpers
• check_box_tag
• radio_button_tag
• No _tag • text_area_tag
• password_field_tag
• hidden_field_tag
15. 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"/>
16. 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_field :title %>
<%= f.text_area :body, :size => "60x12" %>
<%= f.submit "Create" %>
<% end %>
20. fields_for
has_one
or
Person Contact
has_many
•name •address
•age •telephone
•sex
belong_to
using nested attributes
21. fields_for
class Person < ActiveRecord::Base
has_one :contact, :dependent => :destroy
end
class Contact < ActiveRecord::Base
belongs_to :person
end
using accepts_nested_attributes_for
22. 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)
23. Using Namespaces
namespace :admin do
resources :posts, :comments
end
in routes.rb
form_for [:admin, @post]
in _form.html.erb
admin_post_path(@post) on update
app/controllers/admin/posts_controller.rb
app/controllers/admin/comments_controller.rb
app/views/admin/posts/new.html.erb
app/views/admin/posts/edit.html.erb