Skip to content

8 Form Generator

ovoice edited this page Jun 22, 2011 · 4 revisions

Form Generator

There’s a Formtastic form code generator to make your transition to Formtastic easier. All you have to do is to specify an existing model name, and optionally specify view template framework (ERB/HAML), and you are good to go. Note: This won’t overwrite any of your stuff. This is how you use it:

Alt. 1: Generate in terminal:

$ ./script/generate form Post
# ---------------------------------------------------------
#  GENERATED FORMTASTIC CODE
# ---------------------------------------------------------

<% f.inputs do %>
  <%= f.input :title, :label => 'Title' %>
  <%= f.input :body, :label => 'Body' %>
  <%= f.input :published, :label => 'Published' %>
<% end %>

# ---------------------------------------------------------
 Copied to clipboard - just paste it!

In rails 3:

rails g formtastic:form Post

Alt. 2: Generate partial:

$ ./script/generate form Post --partial
      exists  app/views/posts
      create  app/views/posts/_form.html.erb

To generate HAML markup, just add the --haml as argument:

$ ./script/generate form Post --haml
      exists  app/views/admin/posts
      create  app/views/admin/posts/_form.html.haml

To specify the controller in a namespace (eg admin/posts instead of posts), use the —controller argument:

$ ./script/generate form Post --partial --controller admin/posts
      exists  app/views/admin/posts
      create  app/views/admin/posts/_form.html.erb
Something went wrong with that request. Please try again.