Ruby on Rails - Form Helpers
Introduction
Form helpers in Rails simplify the process of creating forms and managing form data. They provide a convenient way to generate form elements and handle form submissions, ensuring your forms are secure and easy to use. This guide will cover the basics of using form helpers in Rails views.
Key Points:
- Form helpers generate form elements and handle form submissions.
- They ensure that forms are secure by including authenticity tokens.
- This guide covers the most commonly used form helpers in Rails.
Using form_with
The form_with
helper is the primary way to create forms in Rails. It automatically generates the appropriate form tag and includes an authenticity token for security. Here is a basic example:
<!-- app/views/articles/new.html.erb -->
<%= form_with model: @article do |form| %>
<div>
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<div>
<%= form.submit 'Save' %>
</div>
<% end %>
In this example, a form is created for the @article
model with fields for the title and body, and a submit button.
Common Form Helpers
Rails provides several form helpers to generate different types of form elements. Here are some of the most commonly used form helpers:
text_field
: Creates a text input field.text_area
: Creates a text area.password_field
: Creates a password input field.hidden_field
: Creates a hidden input field.file_field
: Creates a file input field.check_box
: Creates a checkbox.radio_button
: Creates a radio button.select
: Creates a dropdown select box.date_select
: Creates a date select dropdown.submit
: Creates a submit button.
Example Form with Common Helpers
Here is an example of a form using various form helpers:
<!-- app/views/users/new.html.erb -->
<%= form_with model: @user do |form| %>
<div>
<%= form.label :username %>
<%= form.text_field :username %>
</div>
<div>
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div>
<%= form.label :password %>
<%= form.password_field :password %>
</div>
<div>
<%= form.label :gender %>
<%= form.radio_button :gender, 'male' %> Male
<%= form.radio_button :gender, 'female' %> Female
</div>
<div>
<%= form.label :terms_of_service %>
<%= form.check_box :terms_of_service %>
</div>
<div>
<%= form.label :country %>
<%= form.select :country, ['USA', 'Canada', 'UK'] %>
</div>
<div>
<%= form.submit 'Sign Up' %>
</div>
<% end %>
Handling File Uploads
To handle file uploads, use the file_field
helper and ensure your form has the appropriate encoding type. Here is an example:
<!-- app/views/users/edit.html.erb -->
<%= form_with model: @user, local: true, html: { multipart: true } do |form| %>
<div>
<%= form.label :avatar %>
<%= form.file_field :avatar %>
</div>
<div>
<%= form.submit 'Update Profile' %>
</div>
<% end %>
Using Form Builders
Form builders provide an easy way to generate forms for complex objects. Rails comes with the FormBuilder
class that can be customized for specific needs. Here is an example of a custom form builder:
# app/helpers/custom_form_builder.rb
class CustomFormBuilder < ActionView::Helpers::FormBuilder
def text_field(attribute, options = {})
label(attribute) + super
end
end
# app/views/articles/new.html.erb
<%= form_with model: @article, builder: CustomFormBuilder do |form| %>
<%= form.text_field :title %>
<%= form.text_area :body %>
<%= form.submit 'Save' %>
<% end %>
Conclusion
Form helpers in Rails make it easy to create forms and handle form data securely and efficiently. By using the various form helpers provided by Rails, you can build complex forms quickly and ensure they are maintainable and secure.