#184
Oct 19, 2009

Formtastic Part 1

Formtastic is a concise way to generate form views. In this episode (part 1 of 2) I show how to generate both a simple form and a more complex one which is customized with options.
Download (19.7 MB, 10:41)
alternative download for iPod & Apple TV (12.7 MB, 10:41)

Resources

rails vet
sudo rake gems:install
script/generate nifty_layout
script/generate nifty_scaffold category name:string description:text
rake db:migrate
script/generate formtastic_stylesheets
script/generate nifty_scaffold animal name:string category_id:integer born_on:date female:boolean
<!-- application.html.erb -->
<%= stylesheet_link_tag 'application', 'formtastic', 'formtastic_changes', :cache => "base" %>

<!-- views/animals/_form.html.erb -->
<% semantic_form_for @category do |f| %>
  <%= f.inputs %>
  <%= f.buttons %>
<% end %>

<!-- views/animals/_form.html.erb -->
<% semantic_form_for @animal do |f| %>
  <% f.inputs do %>
    <%= f.input :name %>
    <%= f.input :born_on, :start_year => 1900 %>
    <%= f.input :category, :include_blank => false %>
    <%= f.input :female, :as => :radio, :label => "Gender", :collection => [["Male", false], ["Female", true]] %>
  <% end %>
  <%= f.buttons %>
<% end %>

RSS Feed for Episode Comments 45 comments

1. Ryan Oct 19, 2009 at 00:09

Yay for mondays and railscasts! Love your work! Thanks alot!


2. Steve Oct 19, 2009 at 00:19

Yes! I was actually looking for a form creator like this yesterday... Can't wait for next week. =)


3. QuBiT Oct 19, 2009 at 01:11

Nice one, really handy and I am eager to hear and see more soon ^^

btw:
Garfield was born in the kitchen of Mama Leoni's Italian Restaurant (June 19, 1978) and immediately ate all the pasta and lasagna in sight ^^


4. Samuel Tonini Oct 19, 2009 at 01:11

A coffee and your cast that I'm calling a good Monday. ;) Thanks


5. David Oct 19, 2009 at 02:36

hehe same for me, coffee+railscast=good start of the week

thx ryan :)


6. Martin Oct 19, 2009 at 02:51

@Samuel that's true. Again a great screencast!


7. Ariejan de Vroom Oct 19, 2009 at 03:02

I've been using formtastic for a while now and it's really awesome!

Thanks for this screencast, Ryan!


8. stalkert Oct 19, 2009 at 03:11

Really great cast, thx again Ryan!


9. Justin French Oct 19, 2009 at 03:13

Ryan, I really don't think I could've asked for a better introduction to Formtastic. I watched it with a huge smile. Looking forward to part 2!

Thanks,
Justin


10. Dag Oct 19, 2009 at 04:57

It requires XHTML? That's crazy! XHTML must be served as text/html to get acceptable browser support, and then it renders worse than HTML4.


11. luciano Oct 19, 2009 at 05:22

Ryan, as usual, you are making our mondays very much interesting! thanks for another great cast!
Thanks
Luciano


12. Marcelo Oct 19, 2009 at 05:33

Very interesting! Makes our work even faster huh? Looking forward for part II.

Ryan, you could make a list of your favorite plugins/gems you use. What you think? We now have a lot of options to use with our Rails projects and is getting harder to make a choice. Thanks.


13. Joachim Oct 19, 2009 at 05:37

Great episode! Just a small remark: Wouldn't it better to use gemcutter.org as gem source? The newest version of Formtastic is v0.2.5 which is not (and never will be) available on gems.github.com.


14. Alex Reisner Oct 19, 2009 at 06:28

For another approach to forms, check out my Informant gem:

http://github.com/alexreisner/informant

Thanks Ryan!


15. Ryan Bates Oct 19, 2009 at 07:07

@Justin, glad you like it! Keep up the great work with Formtastic.

@Joachim, you're right, the gemcutter source would be better. I actually recorded this episode a few weeks ago, and I'm not sure if that was available at the time.


16. backspace Oct 19, 2009 at 07:35

Neato! You could use category:references when generating the animal model, which would add the belongs_to call automatically.


17. Soleone Oct 19, 2009 at 08:31

Great episode like always! I was really impressed by how easy the customization is. And thanks for the laugh: "Cuddly, but Deadly", hehe, straight to the point :)


18. Papz Oct 19, 2009 at 08:38

Awsome! Thanks! I will use that in my app right now! Again: thanks!


19. Lin He Oct 19, 2009 at 10:12

Nice episode on forms. Thanks Ryan! thanks Justin for this plugin. It's really handy


20. Eric Berry Oct 19, 2009 at 10:17

Thank you!!! I've been hoping to see this one. Great job! Keep up the awesome work


21. Josh Cooper Oct 19, 2009 at 15:38

Ryan,

I hang out monday afternoons (aus time) waiting for your rails casts and it is never a dissapointment... awesome work on this one... i can't wait to use it for our projects.


22. xn Oct 20, 2009 at 09:29

If not covered in part two, maybe a part three covering the usage of Searchlogic and Formtastic in tandem?

Thanks for the work.


23. Yuval Oct 20, 2009 at 14:56

Cute but perhaps a bit gratuitous. I prefer to use form builders wrapped in a helper method. Cheers.


24. Shreyans Oct 20, 2009 at 17:34

I know it is not good to ask this question here, but I can't stop myself from doing this coz I need this function immediately.

Does anyone know how to send formatted output to printer using Ruby?
Or
Is there a way to send a formatted pdf document to a printer using Ruby?

I need suggestions. Please help.
Ryan Bates rocks....


25. Dave Oct 20, 2009 at 19:41

Shreyans,

Post your question on www.stackoverflow.com. You'll probably get an answer more quickly than here :)


26. Ken Riley Oct 21, 2009 at 16:21

I was thinking about something very much like this last night. The difference is that I was thinking of something that would loop through all existing models in a project and generate the forms. This looks great on its own, but also looks like a fantastic starting point for a generate-new-forms plugin!


27. nick Oct 21, 2009 at 17:48

Great comparison between traditional scaffold and formtastic.
I would like to see how validation works in formtastic.


28. Bharat Oct 21, 2009 at 18:35

Hey Ryan,
Great job as usual. Please make sure to cover Formtastic in at least 3 or 4 episodes. It is that good.


29. grimen Oct 21, 2009 at 19:40

Nice cast!

@nick:

Validation error messages is attached to the fields instead all in one huge block, and you can render general form errors using errors[:base] - pure Rails.

FYI: I'm a heavy Formtastic user and created a gem that handles client-side (and fallback on AJAX when client-side not possible) validations of forms by extracting needed validation info from your models. Formtastic markup in mind, so it's compatible out of the box. Actually compatible with any markup if you want. Check it out:

http://github.com/grimen/validatious-on-rails

Unobtrusive and JS framework agnostic. =)


30. Rick DeNatale Oct 25, 2009 at 09:17

I just tried out the formtastic textmate bundle, and notice that the ftgem snippet still generates a source option for the gem as github.

I sent a message to the author of the bundle via git about that. If there's time, you might want to mention this in the next railscast.

Also I just reported lots of spam comments on this episode. I don't think most readers would really be interested in ugg shoes, rolex watches or tiffany stuff, fake or not <G>


31. grimen Oct 25, 2009 at 15:07

@Rick DeNatale:

Gemcutter is now default. =)


32. harini Oct 29, 2009 at 21:54

How to apply the same admin side, Unable to do nifty scaffolding on admin side. ruby script/generate nifty_scaffold 'admin/authors'???


33. Dave Oct 30, 2009 at 09:22

After install in a fresh rails structure and then running

gem sources -a http://gemcutter.org/

   and then

gem install formtastic

I get this error after editing my form:

undefined method `semantic_form_for' for #<ActionView::Base:0x64bf054>

The formtastic.css shows up ok and I modified the layout to include this. I've tried it with standard and nifty scaffolds. Any ideas for the error? I'm thinking it is something with the gem and my configuration. But I'm new to rails and clueless.


34. Eric L Nov 02, 2009 at 10:42

I think a minor correction in the code snippets is needed.

The comment for the first form should be:
<!-- views/categories/_form.html.erb -->

It is showing as views/animals/... for both form snippets.


35. Kevin Nov 02, 2009 at 15:53

Same as Dave

After installing formtastic and altering forms:

undefined method `semantic_form_for' for #<ActionView::Base:0x64bf054>


36. Chris Jobling Nov 06, 2009 at 02:42

Ryan

Looks like you've had a herd of trained monkeys who've managed to post nearly 50 link spam comments to your site. Must be human to get through the Captcha.


37. von Nov 06, 2009 at 15:43

Hi,

Can you help me with this error :

uninitialized constant Formtastic::SemanticFormBuilder::I18n

Extracted source (around line #6):
3: <%= error_messages_for :task %>
4:
5: <% semantic_form_for(@task) do |f| %>
6: <%=f.inputs %>
7: <%=f.buttons %>
8: <% end %>
9:


38. J. Pablo Fernández Nov 21, 2009 at 18:06

Don't use justinfrench-formtastic, that's too old. Use formtastic from gemcutter.


39. uggs outlet Dec 10, 2009 at 23:33

Good sharing. I like your post. Thanks for share your thoughts.
Wholesale and retail are both acceptable to us. Welcome to our site and free to look! Thank you and wish you a

nice day. Good Luck!


40. T Dec 28, 2009 at 02:25

T


41. yeni müzik Dec 29, 2009 at 15:48

This is one of those "clever" solutions that will only cause


42. Justin French Jan 03, 2010 at 00:35

The usual reasons for "undefined method `semantic_form_for'" are:

* you haven't added the gem to your environment.rb
* you forgot to restart your server after addding the gem to the environment (or installing the plugin)
* you have config.plugins set in your environment to load plugins in a specific order, but it doesn't include formtastic


43. Dennis Jan 08, 2010 at 01:48

Just wanted to say: thanks for all the superb tutorials! Keep up the good work!


45. Links of London jewelry Jan 11, 2010 at 23:27

I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day!


46. Manolo Jan 24, 2010 at 23:05

Hi,

Can you help me with this error :

uninitialized constant Formtastic::SemanticFormBuilder::I18n

Extracted source (around line #6):
3: <%= error_messages_for :task %>
4:
5: <% semantic_form_for(@task) do |f| %>
6: <%=f.inputs %>
7: <%=f.buttons %>
8: <% end %>
9:

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source