Daniel May
Daniel May

Reputation: 2262

Good forms helpers for Sinatra?

I am starting to do forms and am looking for form helpers in Sinatra.

Ideally, I'm after just a set of decent forms helpers that I can include as a gem and just start using, in place of hand-rolling all the base level erb/haml/ruby forms building.

What are your recommendations?

Upvotes: 4

Views: 4462

Answers (2)

Cymen
Cymen

Reputation: 14429

I recommend using sinatra-formhelpers-ng because it fixes a bug in sinatra-formhelpers.

I too found sinatra-formhelpers useful but it didn't appear to be maintained and I ran into a bug: the state of SELECT tags is not persisted across form submissions. So in other words, if I make a form with a bunch of fields and two dropdown lists (SELECT tags), all the fields are persisted if I say want to rerender the form in the POST due to failing validation except the SELECT tags. I fixed this and pushed it to the original sinatra-formhelpers repo. The push was accepted but the gem was not updated. I waited a couple weeks and then forked it to sinatra-formhelpers-ng.

Upvotes: 4

Patrick Oscity
Patrick Oscity

Reputation: 54734

I find sinatra-formhelpers useful and used it in some projects. Have a look at their Github page, the code is pretty straightforward and might just be what you are looking for. Even if not, you could add your own specialized helpers easily. You can simply install it with

gem install sinatra-formhelpers

and use it by requiring the Gem:

require 'sinatra/form_helpers'

or, if you subclass Sinatra::Base, by additionally including the helpers:

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

After all, part of Sinatra's philosophy is to be as light as possible. So if you want all the fancy things built in, Sinatra might just not be the right tool.

Upvotes: 3

Related Questions