Save 37% off PRO during our Black Friday Sale! »

Rails Essentials: simple_form

Rails Essentials: simple_form

Houston Ruby Brigade appetizer to simple_form (rails form DSL)

3b5cb0411cbd1012acab1b60f7ce3606?s=128

Jesse Wolgamott

October 10, 2012
Tweet

Transcript

  1. SIMPLE FORM Rails Essentials: @jwo

  2. gem “simple_form” bundle rails g simple_form:install By Platformatec (same cats

    as Devise)
  3. rails g scaffold post title body publish_on:date featured:boolean Why is

    this needed? <%= simple_form_for(@post) do |f| %> <%= f.error_notification %> <div class="form-inputs"> <%= f.input :title %> <%= f.input :body %> <%= f.input :publish_on %> <%= f.input :featured %> </div> <div class="form-actions"> <%= f.button :submit %> </div> <% end %>
  4. As Compared to <%= form_for(@post) do |f| %> <% if

    @post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> <ul> <% @post.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :body %><br /> <%= f.text_field :body %> </div> <div class="field"> <%= f.label :publish_on %><br /> <%= f.date_select :publish_on %> </div> <div class="field"> <%= f.label :featured %><br /> <%= f.check_box :featured %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
  5. Let’s see that again <%= simple_form_for(@post) do |f| %> <%=

    f.error_notification %> <div class="form-inputs"> <%= f.input :title %> <%= f.input :body %> <%= f.input :publish_on %> <%= f.input :featured %> </div> <div class="form-actions"> <%= f.button :submit %> </div> <% end %>
  6. It’s Just a Wrapper All form_helpers in Rails apply select,

    collection_select, date_select You can pass to the HTML of the form or the input
  7. Why over Formtastic? Formtastic had a strong opinion on form

    markup created You can define what markup to use with your CSS Twitter Bootstrap, Zurb Foundation, ALL CAN BE YOURS
  8. GUESS WHAT

  9. Associations has many throughs are now SIMPLE. f.association :categories, as:

    :check_boxes
  10. Bootstrap Integration Yes, it does this nicely And there’s a

    bootstrap picker. demo (github.com/houstonruby/rails_essentials/simple_form