New to Rails 3? Check out the Ruby on Rails 3 Tutorial book and screencast.

A book and screencast series showing you how to develop and deploy industrial-strength Rails apps in a direct, step by step way. The screencast series includes 12 lessons over more than 15 hours! Get the best "over the shoulder" experience of following what a top Rails 3 developer does when building an app today. Click here to learn more.

Formtastic: Forms Made Crazy Easy for Rails Developers

In Plugins

html-forms.pngFormtastic 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.

CodebaseLogo-RI.png[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.

Post to Twitter Tweet This Post

Vaguely Related Posts (Usually)

13 Comment Responses to “Formtastic: Forms Made Crazy Easy for Rails Developers”

  1. #1
    Peter Cooper Says:

    So.. ignore the crazy coloring on the code sample. Not sure what's up with that!

  2. #2
    grimen Says:

    Yea, the form example code is not generated. Formtastic is DRY, but not that DRY. ;)

  3. #3
    grimen Says:

    Some relevant and useful additions:

    SASS-stylesheets - for even more pretty styled forms in all browsers (mixins for different form layouts, etc)

    TextMate-bundle for Formtastic:

    In-place editor for Formtastic:

    Client-side validations that is compatible with Formtastic markup (warning: shameless self-promotion):

  4. #4
    Peter Cooper Says:

    grimen: I didn't realize there was so much stuff for Formtastic already - thanks for linking us up!

  5. #5
    Marcelo Silveira Says:

    Great stuff. Anyone considered moving it into core?

  6. #6
    Peter Cooper Says:

    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.

  7. #7
    grimen Says:

    @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. =)

  8. #8
    Justin French Says:

    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 ;)

  9. #9
    Peter Cooper Says:

    Now I know what you meant grimen, I just switched to Safari and the code is not there. How bizarre!

  10. #10
    Peter Cooper Says:

    Fixed the code so it works everyplace. For some reason the opening brackets weren't being turned into HTML entities.

  11. #11 » Blog Archive » Rails Reading - Oct 7, 2009 Says:

    [...] Formtastic: Forms Made Crazy Easy for Rails Developers [...]

  12. #12
    TomR Says:

    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

  13. #13
    Bob Says:

    @TomR: I had the same problem. Try stopping/restarting your server after installing the gem.

Leave a Reply