Formtastic: Forms Made Crazy Easy for Rails Developers
Formtastic is a Rails plugin by Justin French that aims to take the headaches out of building forms in Rails views. To build it, Justin wrote down how he'd like a form creation DSL to look and then worked backwards to building the code necessary to implement that DSL. The result is a very obvious and straightforward form creation DSL.
Formtastic doesn't just make it easy to whip up basic forms, though. It has some significant advantages over similar plugins:
- It's Rails 2.3 ready
- It's under active development (the last update was yesterday)
- It supports internationalization
- It has full spec coverage
- It can handle belongs_to, has_many, and has_and_belongs_to_many associations out of the box, rendering multi-selects and radio inputs where necessary!
- It doesn't screw around with the existing Rails form helpers
Here's a large form example straight from Formtastic's documentation (which is pretty thorough, you'll be glad to know):
<% semantic_form_for @article do |form| %> <% form.inputs :name => "Basic" do %> <%= form.input :title %> <%= form.input :body %> <%= form.input :section %> <%= form.input :publication_state, :as => :radio %> <%= form.input :category %> <%= form.input :allow_comments, :label => "Allow commenting on this article" %> <% end %> <% form.inputs :name => "Advanced" do %> <%= form.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %> <%= form.input :extract, :required => false %> <%= form.input :description, :required => false %> <%= form.input :url_title, :required => false %> <% end %> <% form.inputs :name => "Author", :for => :author do |author_form| %> <%= author_form.input :first_name %> <%= author_form.input :last_name %> <% end %> <% form.buttons do %> <%= form.commit_button %> <% end %> <% end %>
The result is a clean bundle of HTML. Groovy, right? So get reading the documentation and give it a try. It's an awesome plugin.
[ad] Codebase is a fast & reliable git, mercurial & subversion hosting service with complete project management built-in - ticketing, milestones, wikis & time tracking - all under one roof. Click here to try it - free.
October 5th, 2009 at 12:01 pm
So.. ignore the crazy coloring on the code sample. Not sure what's up with that!
October 5th, 2009 at 12:28 pm
Yea, the form example code is not generated. Formtastic is DRY, but not that DRY. ;)
October 5th, 2009 at 12:36 pm
Some relevant and useful additions:
SASS-stylesheets - for even more pretty styled forms in all browsers (mixins for different form layouts, etc)
http://github.com/activestylus/formtastic-sass
TextMate-bundle for Formtastic:
http://github.com/grimen/formtastic_tmbundle
In-place editor for Formtastic:
http://github.com/rubymood/jintastic
Client-side validations that is compatible with Formtastic markup (warning: shameless self-promotion):
http://github.com/grimen/validatious-on-rails
October 5th, 2009 at 12:58 pm
grimen: I didn't realize there was so much stuff for Formtastic already - thanks for linking us up!
October 5th, 2009 at 2:23 pm
Great stuff. Anyone considered moving it into core?
October 5th, 2009 at 3:21 pm
As with "sexy migrations", I suspect this is the sort of thing that could make core one day. It probably depends on which way the wind is swinging with their alternating "let's bundle everything" vs "put everything in a plugin" philosophies.
October 5th, 2009 at 3:39 pm
@Peter: Yes, totally agree. I do believe that Rails core should continue follow the principle: If *almost everyone* is re-writing the helpers is a similar way for similar reasons, something is wrong. Continuesly everyday re-inventing belongs to the PHP world - for me personally, the past. =)
October 5th, 2009 at 6:30 pm
Thanks for the kind words! I'd love to see all this in core one day, but we still have plenty to before it's at the level where I'd consider lobbying to get it in, but trust me, I will be one day ;)
October 6th, 2009 at 9:25 am
Now I know what you meant grimen, I just switched to Safari and the code is not there. How bizarre!
October 6th, 2009 at 9:28 am
Fixed the code so it works everyplace. For some reason the opening brackets weren't being turned into HTML entities.
October 7th, 2009 at 10:24 am
[...] Formtastic: Forms Made Crazy Easy for Rails Developers [...]
November 3rd, 2009 at 11:01 pm
I have installed formtastic as per instructions from github. The gem(s) installed (v 0.9.1) However, trying to use it I get no method error (semantic_form_for) What have I missed
November 7th, 2009 at 4:08 pm
@TomR: I had the same problem. Try stopping/restarting your server after installing the gem.