- Use FormHelper methods when you are using a form
to edit a row in a database, or to add a new row to the database.
- Use FormTagHelper methods when the piece of data you are inputing does
not directly correspond to any field in the database.
- FormHelper methods process an entire database row at a time;
FormTagHelper methods process separately each piece of data coming from
or going to a control.
- References:
api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
- Form
FormTagHelper Version:
<% form_tag 'show' do %>
... Controls go here.
<% end %>
FormHelper Version:
<% form_for @item, :url => { :action => 'show' } do |f| %>
... Controls go here.
<% end %>
- Textfield
FormTagHelper Version:
<%= text_field_tag :celsius, '20', :class => 'tf' %>
:celsius is the name; '20' is the initial value.
FormHelper Version:
<%= f.text_field :celsius, :class => 'tf' %>
:celsius is the field of the database object f.
- Dropdown Menu
FormTagHelper Version:
<%= select_tag :source_rate,
options_for_select([['Dollars', 1.00000],
['Euro', 1.36749],
['Pesos', 12.6149],
['Pounds', 1.58228]]),
:class => 'ddm' %>
:source_rate is the name;
the first column of the array are the values displayed in the
dropdown menu; the second column are the values returned to the
controller to be read by a params statement.
the option tags are the HTML option tags.
FormHelper Version:
<%= f.select_tag :source_rate,
[['Dollars', 1.00000],
['Euro', 1.36749],
['Pesos', 12.6149],
['Pounds', 1.58228]],
:class => 'ddm'
:source_rate is the field of the database object f;
the values of the dropdown menu are specified in the array as they were
in the FormTagHelper version; note that for the FormHelper version,
no options_for_select method is needed.
- Radio Button
FormTagHelper Version:
<%= radio_button_tag :group, value, true %>
:group sets the button group (in a group of radio buttons, only can be
checked at a time)
:value is the value returned to the controller
to be read by a params statement
true means that the radio button should be
initially checked.
FormHelper Version:
<%= f.radio_button :group, value, :checked => true %>
:group sets the button group (in a group of radio buttons, only can be
checked at a time)
value is the value returned to the controller
to be read by a params statement
:checked => true means that the radio button should be
initially checked.
- CheckBox
FormTagHelper Version:
<%= check_box_tag :sugar, 1, true, :class => 'cb' %>
:sugar is the name of the checkbox
1 is the value returned to the controller in a params statement
false means that the checkbox is initially checked.
FormHelper Version:
<%= f.check_box :sugar, 1, :checked => true, :class => 'cb' %>
:sugar is the name of the checkbox
1 is the value returned to the controller in a params statement,
0 will be returned if the box is not checked.
:checked => true means that the checkbox is initially checked.