Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cymen/sinatra-formhelpers-ng
Lightweight, robust form helpers for Sinatra
https://github.com/cymen/sinatra-formhelpers-ng
Last synced: 3 months ago
JSON representation
Lightweight, robust form helpers for Sinatra
- Host: GitHub
- URL: https://github.com/cymen/sinatra-formhelpers-ng
- Owner: cymen
- License: other
- Fork: true (twilson63/sinatra-formhelpers)
- Created: 2013-03-28T04:06:20.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-05-31T20:55:33.000Z (over 9 years ago)
- Last Synced: 2024-05-31T20:01:11.666Z (6 months ago)
- Language: Ruby
- Homepage: http://github.com/twilson/sinatra-formhelpers
- Size: 267 KB
- Stars: 12
- Watchers: 3
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Sinatra::FormHelpers - Lightweight form helpers for Sinatra
===========================================================**IMPORTANT: THIS FORK IS NO LONGER MAINTAINED. You can find the maintained version of this gem [here](https://github.com/duijf/sinatra-formhelpers-ng).**
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" # google
label :person, :first_name # First Name
input :person, :first_name #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, andnested[names]
to make ORMs happy
3. No Rails ultra-magic(tm) here. Just fast, simple code.Usage
-----
With Bundler/Isolate:gem 'sinatra-formhelpers-ng'
Then, include it in a Sinatra application:
require 'sinatra/form_helpers'
If you're subclassing
Sinatra::Base
, you also need to callhelpers
manually:class MyApp < Sinatra::Base
helpers Sinatra::FormHelpers
# ...
endViews
-----
In your views, use these helpers to dynamically create form HTML elements. Here's an example in ERB:
Fill out the below form to sign up.
For more information, visit our <%= link 'FAQ', '/faq' %>
<%= 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, theform()
helper just takes a URL and method. (Note thatform()
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 %><%= 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.
Known Bugs
----------
* Currentlyfieldset
does not return a tag properly.Fixed Bugs
----------
* The state of select tags was not persisted across form submissions.Authors
-------
* [Initial efforts](https://github.com/twilson63/sinatra-formhelpers) (c) 2009 [Tom Wilson](https://github.com/twilson63).
* [Additional efforts](https://github.com/nateware/sinatra-formhelpers) (c) 2011 [Nate Wiger](http://nateware.com).
* Further efforts (c) 2013 [Cymen Vig](http://blog.cymen.org/).