No commit activity in last 3 years
No release in over 3 years
Simple, lightweight form helpers for Sinatra.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 0.6
~> 10.3
~> 3.1
~> 0.9
~> 1.4
 Project Readme

Sinatra::FormHelpers - Lightweight form helpers for Sinatra

Build Status

This plugin adds lightweight (3-5 lines each) form helpers to Sinatra that aid with common form and HTML tags.

link "google", "http://www.google.com"  # <a href="http://www.google.com">google</a>
label :person, :first_name              # <label for="person_first_name">First Name</label>
input :person, :first_name              # <input name="person[first_name]" id="person_first_name" type="text" />

There are also helpers for: form, textarea, submit, image, radio, checkbox, and select

Why Bother?

After all, you can just write Haml or write your own helpers or hand-code raw HTML or whatever. Well, here's some considerations:

  1. Helpers maintain correct state across form submissions (eg, on errors, form stays filled in)
  2. Generate automatic labels, valid CSS ID's, and nested[names] to make ORMs happy
  3. No Rails ultra-magic(tm) here. Just fast, simple code.

Usage

With Bundler/Isolate:

gem 'sinatra-formhelpers-huii'

Then, include it in a Sinatra application:

require 'sinatra/form_helpers'

If you're subclassing Sinatra::Base, you also need to call helpers manually:

class MyApp < Sinatra::Base
  helpers Sinatra::FormHelpers
  # ...
end

Views

In your views, use these helpers to dynamically create form HTML elements. Here's an example in ERB:

<p>
  Fill out the below form to sign up.
  For more information, visit our <%= link 'FAQ', '/faq' %>
</p>

<%= form('/users', :post) %>

<%= input(:user, :first_name) %>
<%= input(:user, :last_name) %>

<%= input(:user, :email, :size => 40) %>

<%= password(:user, :password) %>
<%= password(:user, :confirm_password) %>

<%= radio(:user, :gender, ['M', 'F']) %>

<%= submit %>

Unlike the super-magic Rails form_for method, the form() helper just takes a URL and method. (Note that form() will accept :create, :update, and :delete and include the special _method hidden param for you.)

To reduce repetition, use fieldset() to prefix fields with a namespace:

<%= form('/users', :create) %>

<% fieldset(:user) do |f| %>
  <%= f.input(:first_name) %>
  <%= f.input(:last_name) %>

  <%= f.input(:email, :size => 40) %>

  <%= f.password(:password) %>
  <%= f.password(:confirm_password) %>

  <%= f.radio(:gender, ['M', 'F']) %>
<% end %>

</form> <-- You need to close it manually if you are not passing a block

<%= submit 'Create account' %>
<%= submit 'Cancel', :onclick => 'window.location=http://mydomain.com;return false' %>

or

<%= form(:user, :post, action: '/users') do |f| %> <-- This will create a fieldset automatically
  <%= f.input(:first_name) %>
  <%= f.input(:last_name) %>

  <%= f.input(:email, :size => 40) %>

  <%= f.password(:password) %>
  <%= f.password(:confirm_password) %>

  <%= f.radio(:gender, ['M', 'F']) %>
<% end %>

<%= submit 'Create account' %>
<%= submit 'Cancel', :onclick => 'window.location=http://mydomain.com;return false' %>

This will create fields named user[first_name], user[last_name], and so forth.

List of helpers so far

  • form
  • fieldset
  • link
  • image
  • label
  • input
  • password
  • email
  • textarea
  • submit
  • reset
  • button
  • checkbox
  • radio
  • select
  • hidden

Known Bugs

  • fieldset must be optional in form
  • <%= form(:user, :post, action: '/users') do |f| %> doesn't work in eRB, but works in Slim. Crap. :/

Fixed Bugs

  • Currently fieldset does not return a tag properly.
  • The state of select tags was not persisted across form submissions.

Running tests

Just run bundle exec rake or bundle exec rspec or rspec.

Authors