Skip to content

Instantly share code, notes, and snippets.

@sfnewzgirl
Created October 13, 2016 01:40
Show Gist options
  • Save sfnewzgirl/4a6cb24aa61f76c0de0ad7d5455ee896 to your computer and use it in GitHub Desktop.
Save sfnewzgirl/4a6cb24aa61f76c0de0ad7d5455ee896 to your computer and use it in GitHub Desktop.
Ruby Lightning Talk on SimpleForm gem

SimpleForm

SimpleForm is meant to streamline the code you write for making a form.

Why use it?

  • Less code!

  • Still looks good!

Example:

Remember the blog tutorial from the Ruby on Rails documentation? Let's change it up with SimpleForm. This is the form to create a new blog article:

<%= form_for @article do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

Now here is how you would make the same form with SimpleForm:

<%= simple_form_for(@article, html: {class: '.form-horizontal'}) do |f| %>

    <%= f.input :title, placeholder: 'Title' %><br>
    <%= f.input :text, placeholder: 'Write an article here' %><br>
    <%= f.button :submit %>

<% end %>

There are actually even more ways to simplify your code.

Set up

Add it to your Gemfile

Or install the gem

Run bundle install

Run the generator

It creates the files then tells you what to code in the console! Then simply refer to the docs for more.

Resources

  • [SimpleForm GitHub](https://github.com/plataformatec/simple_form)
  • [SimpleForm And Bootstrap](http://simple-form.plataformatec.com.br/)
  • [Side by Side Forms](http://simple-form-bootstrap.plataformatec.com.br/)
  • Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment