Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

simple_form

> 后端框架
Open source

Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.

8.2K stars0 likes2 views
WebsiteGitHub

About

Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.

Rails forms made easy.

Simple Form aims to be as flexible as possible while helping you with powerful components to create your forms. The basic goal of Simple Form is to not touch your way of defining the layout, letting you find the better design for your eyes. Most of the DSL was inherited from Formtastic, which we are thankful for and should make you feel right at home.

INFO: This README refers to Simple Form 5.0. For older releases, check the related branch for your version.

Table of Contents

  • Installation
    • Bootstrap
    • Zurb Foundation 5
    • Country Select
  • Usage
    • Stripping away all wrapper divs
    • Collections
    • Priority
    • Associations
    • Buttons
    • Wrapping Rails Form Helpers
    • Extra helpers
      • Simple Fields For
      • Collection Radio Buttons
      • Collection Check Boxes
  • Available input types and defaults for each column type
  • Custom inputs
  • Custom form builder
  • I18n
  • Configuration
    • The wrappers API
  • Custom Components
  • HTML 5 Notice
    • Using non Active Record objects
  • Information
    • RDocs
    • Supported Ruby / Rails versions
    • Bug reports
  • Maintainers
  • License

Installation

Add it to your Gemfile:

ruby
gem 'simple_form'

Run the following command to install it:

bash
bundle install

Run the generator:

bash
rails generate simple_form:install

Bootstrap 5

Simple Form can be easily integrated with Bootstrap 5. Use the bootstrap option in the install generator, like this:

bash
rails generate simple_form:install --bootstrap

This will add an initializer that configures Simple Form wrappers for Bootstrap 5's form controls. You have to be sure that you added a copy of the Bootstrap assets on your application.

For more information see the generator output, our example application code.

Zurb Foundation 5

To generate wrappers that are compatible with Zurb Foundation 5, pass the foundation option to the generator, like this:

bash
rails generate simple_form:install --foundation

Please note that the Foundation wrapper does not support the :hint option by default. In order to enable hints, please uncomment the appropriate line in config/initializers/simple_form_foundation.rb. You will need to provide your own CSS styles for hints.

Please see the instructions on how to install Foundation in a Rails app.

Country Select

If you want to use the country select, you will need the country_select gem, add it to your Gemfile:

ruby
gem 'country_select'

If you don't want to use the gem you can easily override this behaviour by mapping the country inputs to something else, with a line like this in your simple_form.rb initializer:

ruby
config.input_mappings = { /country/ => :string }

Usage

Simple Form was designed to be customized as you need to. Basically it's a stack of components that are invoked to create a complete html input for you, which by default contains label, hints, errors and the input itself. It does not aim to create a lot of different logic from the default Rails form helpers, as they do a great job by themselves. Instead, Simple Form acts as a DSL and just maps your input type (retrieved from the column definition in the database) to a specific helper method.

To start using Simple Form you just have to use the helper it provides:

erb
<%= simple_form_for @user do |f| %>
  <%= f.input :username %>
  <%= f.input :password %>
  <%= f.button :submit %>
<% end %>

This will generate an entire form with labels for user name and password as well, and render errors by default when you render the form with invalid data (after submitting for example).

You can overwrite the default label by passing it to the input method. You can also add a hint, an error, or even a placeholder. For boolean inputs, you can add an inline label as well:

erb
<%= simple_form_for @user do |f| %>
  <%= f.input :username, label: 'Your username please', error: 'Username is mandatory, please specify one' %>
  <%= f.input :password, hint: 'No special characters.' %>
  <%= f.input :email, placeholder: '[email protected]' %>
  <%= f.input :remember_me, inline_label: 'Yes, remember me' %>
  <%= f.button :submit %>
<% end %>

In some cases you may want to disable labels, hints or errors. Or you may want to configure the html of any of them:

erb
<%= simple_form_for @user do |f| %>
  <%= f.input :username, label_html: { class: 'my_class' }, hint_html: { class: 'hint_class' } %>
  <%= f.input :password, hint: false, error_html: { id: 'password_error' } %>
  <%= f.input :password_confirmation, label: false %>
  <%= f.button :submit %>
<% end %>

It is also possible to pass any html attribute straight to the input, by using the :input_html option, for instance:

erb
<%= simple_form_for @user do |f| %>
  <%= f.input :username, input_html: { class: 'special' } %>
  <%= f.input :password, input_html: { maxlength: 20 } %>
  <%= f.input :remember_me, input_html: { value: '1' } %>
  <%= f.button :submit %>
<% end %>

If you want to pass the same options to all inputs in the form (for example, a default class), you can use the :defaults option in simple_form_for. Specific options in input call will overwrite the defaults:

erb
<%= simple_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f| %>
  <%= f.input :username, input_html: { class: 'special' } %>
  <%= f.input :password, input_html: { maxlength: 20 } %>
  <%= f.input :remember_me, input_html: { value: '1' } %>
  <%= f.button :submit %>
<% end %>

Since Simple Form generates a wrapper div around your label and input by default, you can pass any html attribute to that wrapper as well using the :wrapper_html option, like so:

erb
<%= simple_form_for @user do |f| %>
  <%= f.input :username, wrapper_html: { class: 'username' } %>
  <%= f.input :password, wrapper_html: { id: 'password' } %>
  <%= f.input :remember_me, wrapper_html: { class: 'options' } %>
  <%= f.button :submit %>
<% end %>

Required fields are marked with an * prepended to their labels.

By default all inputs are required. When the form object includes ActiveModel::Validations (which, for example, happens with Active Record models), fields are required only when there is presence validation. Otherwise, Simple Form will mark fields as optional. For performance reasons, this detection is skipped on validations that make use of conditional options, such as :if and :unless.

And of course, the required property of any input can be overwritten as needed:

erb
<%= simple_form_for @user do |f| %>
  <%= f.input :name, required: false %>
  <%= f.input :username %>
  <%= f.input :password %>
  <%= f.button :submit %>
<% end %>

By default, Simple Form will look at the column type in the database and use an appropriate input for the column. For example, a column created with type :text in the database will use a textarea input by default. See the section Available input types and defaults for each column type for a complete list of defaults.

Simple Form also lets you overwrite the default input type it creates:

erb
<%= simple_form_for @user do |f| %>
  <%= f.input :username %>
  <%= f.input :password %>
  <%= f.input :description, as: :text %>
  <%= f.input :accepts,     as: :radio_buttons %>
  <%= f.button :submit %>
<% end %>

So instead of a checkbox for the accepts attribute, you'll have a pair of radio buttons with yes/no labels and a textarea instead of a text field for the description. You can also render boolean attributes using as: :select to show a dropdown.

It is also possible to give the :disabled option to Simple Form, and it'll automatically mark the wrapper as disabled with a CSS class, so you can style labels, hints and other components inside the wrapper as well:

erb
<%= simple_form_for @user do |f| %>
  <%= f.input :username, disabled: true, hint: 'You cannot change your username.' %>
  <%= f.button :submit %>
<% end %>

Simple Form inputs accept the same options as their corresponding input type helper in Rails:

erb
<%= simple_form_for @user do |f| %>
  <%= f.input :date_of_birth, as: :date, start_year: Date.today.year - 90,
                              end_year: Date.today.year - 12, discard_day: true,
                              order: [:month, :year] %>
  <%= f.input :accepts, as: :boolean, checked_value: 'positive', unchecked_value: 'negative' %>
  <%= f.button :submit %>
<% end %>

By default, Simple Form generates a hidden field to handle the un-checked case for boolean fields. Passing unchecked_value: false in the options for boolean fields will cause this hidden field to be omitted, following the convention in Rails. You can also specify include_hidden: false to skip the hidden field:

erb
<%= simple_form_for @user do |f| %>
  <%= f.input :just_the_checked_case, as: :boolean, include_hidden: false %>
  <%= f.button :submit %>
<% end %>

Simple Form also allows you to use label, hint, input_field, error and full_error helpers (please take a look at the rdocs for each method for more info):

erb
<%= simple_form_for @user do |f| %>
  <%= f.label :username %>
  <%= f.input_field :username %>
  <%= f.hint 'No special characters, please!' %>
  <%= f.error :username, id: 'user_name_error' %>
  <%= f.full_error :token %>
  <%= f.submit 'Save' %>
<% end %>

Any extra option passed to these methods will be rendered as html option.

Error messages are HTML-escaped by default. If you need to render HTML in error messages (e.g. links), mark them as html_safe. This works for both custom error messages and model validation messages automatically added to inputs:

erb
<%= f.error :name, error: 'please see <a href="/help">our guide</a>'.html_safe %>

Stripping away all wrapper divs

Simple Form also allows you to strip away all the div wrappers around the <input> field that is generated with the usual f.input. The easiest way to achieve this is to use f.input_field.

Example:

ruby
simple_form_for @user do |f|
  f.input_field :name
  f.input_field :remember_me, as: :boolean
end
xml
<form>
  ...
  <input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text">
  <input name="user[remember_me]" type="hidden" value="0">
  <label class="checkbox">
    <input class="boolean optional" id="user_published" name="user[remember_me]" type="checkbox" value="1">
  </label>
</form>

For check boxes and radio buttons you can remove the label changing boolean_style from default value :nested to :inline.

Example:

ruby
simple_form_for @user do |f|
  f.input_field :name
  f.input_field :remember_me, as: :boolean, boolean_style: :inline
end
xml
<form>
  ...
  <input class="string required" id="user_name" maxlength="255" name="user[name]" size="25

Issues· 37 open

View all issuesOpen on GitHub
  • #1840

    Label's for attribute doesn't match the id when nil is given in collection

    Updated Sep 28, 2026
  • #1848

    `belongs_to` relationships do not show up as required on rails 7.1+

    Updated Feb 7, 2026
  • #1851

    [Bug] Collection option for time zone input doesn't limit the selection

    Updated Sep 10, 2025
  • #1807

    5.2.0 applies `vertical_select` wrapper despite `wrapper: :horizontal_form`

    Updated May 28, 2025
  • #1854

    Add Support for `css-zero`

    Updated May 14, 2025
  • #1850

    need to be able to create accessible input error labels

    Updated Sep 5, 2024
  • #1831

    Proposal for customizable classes merging

    Updated Jun 17, 2024
  • #1739

    Generate HTML5 date/time inputs by default

    Updated Feb 14, 2024
  • #1668

    The state of enums in simple_form

    needs discussionUpdated Dec 11, 2023
  • #1798

    Better support for hyphenated HTML attributes akin to `data` and `aria`

    Updated Dec 2, 2023

> Tags

Rubydslform-builderrailsrails-helper

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架