Skip to content

Working with simple_form

David Wessman edited this page Oct 6, 2015 · 1 revision

simple_form is a form-builder which brings a lot of functionality to our application.

A form can look like this:

<%= simple_form_for(@notice) do |f| %>
  <%= f.input :title %>
  <%= f.input :description, as: :text %>
  <%= f.input :public %>
  <%= f.input :d_publish, as: :datetime_picker %>
  <%= f.input :d_remove, as: :datetime_picker %>
  <%= f.input :sort %>
  <%= f.input :image, as: :file %>
  <%= f.button :submit %>
<% end %>

This will create a wrapping class, it will create classes around each button, it will default to styling defined in config/initializers/simple_form, this makes it easy to have a unified layout.

This handles all the labels and will render errors at each specific field if there is one.

The as:-tag is used to define the input, for most cases it will check the value type and then use the default input for the current type. You can also use custom inputs, for example our datetime_picker defined in app/inputs/datetime_picker.

Make sure that you follow the locale-guide to get your labels localized.