Module: Formtastic::Helpers::FormHelper

Included in:
SemanticFormHelper
Defined in:
lib/formtastic/helpers/form_helper.rb

Overview

FormHelper provides a handful of wrappers around Rails’ built-in form helpers methods to set the ‘:builder` option to `Formtastic::FormBuilder` and apply some class names to the `<form>` tag.

The following methods are wrapped:

  • ‘semantic_form_for` to `form_for`

  • ‘semantic_fields_for` to `fields_for`

  • ‘semantic_remote_form_for` and `semantic_form_remote_for` to `remote_form_for`

The following two examples are effectively equivalent:

<%= form_for(@post, :builder => Formtastic::FormBuilder, :class => 'formtastic post') do |f| %>
  #...
<% end %>

<%= semantic_form_for(@post) do |f| %>
  #...
<% end %>

This simple wrapping means that all arguments, options and variations supported by Rails’ own helpers are also supported by Formtastic.

Since ‘Formtastic::FormBuilder` subclasses Rails’ own ‘FormBuilder`, you have access to all of Rails’ built-in form helper methods such as ‘text_field`, `check_box`, `radio_button`, etc **in addition to** all of Formtastic’s additional helpers like inputs, input, buttons, etc:

<%= semantic_form_for(@post) do |f| %>

  <!-- Formtastic -->
  <%= f.input :title %>

  <!-- Rails -->
  <li class='something-custom'>
    <%= f.label :title %>
    <%= f.text_field :title %>
    <p class='hints'>...</p>
  </li>
<% end %>

Formtastic is a superset of Rails’ FormBuilder. It deliberately avoids overriding or modifying the behavior of Rails’ own form helpers so that you can use Formtastic helpers when suited, and fall back to regular Rails helpers, ERB and HTML when needed. In other words, you’re never fully committed to The Formtastic Way.

Constant Summary collapse

@@builder =

Allows the ‘:builder` option on `form_for` etc to be changed to your own which subclasses `Formtastic::FormBuilder`. Change this from `config/initializers/formtastic.rb`.

Formtastic::FormBuilder
@@default_form_class =

Allows the default class we add to all ‘<form>` tags to be changed from `formtastic` to `whatever`. Change this from `config/initializers/formtastic.rb`.

'formtastic'

Instance Method Summary collapse

Instance Method Details

#semantic_fields_for(record_or_name_or_array, *args, &proc) ⇒ Object

Wrapper around Rails’ own ‘fields_for` helper to set the `:builder` option to `Formtastic::FormBuilder`.

See Also:



150
151
152
# File 'lib/formtastic/helpers/form_helper.rb', line 150

def semantic_fields_for(record_or_name_or_array, *args, &proc)
  form_helper_wrapper(:fields_for, record_or_name_or_array, *args, &proc)
end

#semantic_form_for(record_or_name_or_array, *args, &proc) ⇒ Object

Wrapper around Rails’ own ‘form_for` helper to set the `:builder` option to `Formtastic::FormBuilder` and to set some class names on the `<form>` tag such as `formtastic` and the downcased and underscored model name (eg `post`).

See Rails’ ‘form_for` for full documentation of all supported arguments and options.

Since ‘Formtastic::FormBuilder` subclasses Rails’ own FormBuilder, you have access to all of Rails’ built-in form helper methods such as ‘text_field`, `check_box`, `radio_button`, etc **in addition to** all of Formtastic’s additional helpers like inputs, input, buttons, etc.

Most of the examples below have been adapted from the examples found in the Rails ‘form_for` documentation.

Examples:

Resource-oriented form generation

<%= semantic_form_for @user do |f| %>
  <%= f.input :name %>
  <%= f.input :email %>
  <%= f.input :password %>
<% end %>

Generic form generation

<%= semantic_form_for :user do |f| %>
  <%= f.input :name %>
  <%= f.input :email %>
  <%= f.input :password %>
<% end %>

Resource-oriented with custom URL

<%= semantic_form_for(@post, :url => super_post_path(@post)) do |f| %>
  ...
<% end %>

Resource-oriented with namespaced routes

<%= semantic_form_for([:admin, @post]) do |f| %>
  ...
<% end %>

Resource-oriented with nested routes

<%= semantic_form_for([@user, @post]) do |f| %>
  ...
<% end %>

Rename the resource

<%= semantic_form_for(@post, :as => :article) do |f| %>
  ...
<% end %>

Remote forms (unobtrusive JavaScript)

<%= semantic_form_for(@post, :remote => true) do |f| %>
  ...
<% end %>

Namespaced forms all multiple Formtastic forms to exist on the one page without DOM id clashes and invalid HTML documents.

<%= semantic_form_for(@post, :namespace => 'first') do |f| %>
  ...
<% end %>

Accessing a mixture of Formtastic helpers and Rails FormBuilder helpers.

<%= semantic_form_for(@post) do |f| %>
  <%= f.input :title %>
  <%= f.input :body %>
  <li class="something-custom">
    <label><%= f.check_box :published %></label>
  </li>
<% end %>

Parameters:

  • record_or_name_or_array

    Same behavior as Rails’ ‘form_for`

  • *args (Hash)

    a customizable set of options

See Also:



142
143
144
# File 'lib/formtastic/helpers/form_helper.rb', line 142

def semantic_form_for(record_or_name_or_array, *args, &proc)
  form_helper_wrapper(:form_for, record_or_name_or_array, *args, &proc)
end

#semantic_remote_form_for_wrapper(record_or_name_or_array, *args, &proc) ⇒ Object Also known as: semantic_remote_form_for



164
165
166
167
168
169
170
171
172
# File 'lib/formtastic/helpers/form_helper.rb', line 164

def semantic_remote_form_for_wrapper(record_or_name_or_array, *args, &proc)
  options = args.extract_options!
  if respond_to? :remote_form_for
    semantic_remote_form_for_real(record_or_name_or_array, *(args << options), &proc)
  else
    options[:remote] = true
    semantic_form_for(record_or_name_or_array, *(args << options), &proc)
  end
end