Sinatra::FormHelpers
There are one or two bugs in here at the moment, and I'm currently doing
README-driven-development, which might make using master
a real pain,
especially with HAML. In the meanwhile, check out the docs for 1.9.1
This gem provide lightweight form helpers for Sinatra that will aid you create forms more easily.
Instead of writing this;
%label{ for: 'person_first_name' } First Name
%input{ name: 'person[first_name]' id: 'person_first_name' value: @person.first_name }
with this gem you'll be able to write this:
= label :person, :first_name
= input :person, :first_name
There are many more helpers to choose from, and there are even more benefits to using this. Scroll on!
nested[names]
that make ORMs happy.Install with Bundler by adding the following to your Gemfile
:
gem 'sinatra-formhelpers-ng'
Then, include it in your Sinatra application:
require 'sinatra/form_helpers'
If you are subclassing Sinatra::Base
, in what is referred to as the modular
style, then you need to tell Sinatra where to find the helpers that this
gem provides:
class MyApp < Sinatra::Base
helpers Sinatra::FormHelpers
# ...
end
If you've followed the steps above, you are ready to use the helpers in your views to create the HTML elements. Below you will find a tutorial that also serves as an API reference. This is incomplete for now.
form(:action '/url', &block)
form(:action, "/url", &block)
takes a string, an atom and a block and
returns a string which will contain the form.
The action can be one of :create
, :update
or :delete
. Again, the helper
will open a form and even include the hidden parameter _method
for you (which
helps older browsers implement HTTP verbs like PUT
).
The URL can be anything you want it to be. Just make sure to define an endpoint for it in your Sinatra app.
The block will be evaluated, and will be the content of your form.
= form('/users', :create) do |f|
-# Form code goes here
Will return the following:
<form method="post" action="/url">
<!-- Form content -->
</form>
fieldset(:model, &block)
This subsection and the rest of this section is a TODO.
fieldset
helper currently does not return a <fieldset>
tag properly,
which makes the gem rather useless with HAML. This will get fixed very
soon.Contributions are most welcome! If you are experienced and you know what to do, get cracking!
If you're not, here's your game plan:
git rebase -i master
to reword commit messages, and
resolve merge conflicts if any.If you are unsure about any of the above, but still would like to do something, don't hesitate to send me an email. I can't promise an immediate reaction, but I don't bite.
This project is licenced under the MIT license. You can find a copy in the
LICENSE
file.