class ActionView::Helpers::FormBuilder

Attributes

multipart[R]
multipart?[R]
object[RW]
object_name[RW]
options[RW]
parent_builder[R]

Public Class Methods

_to_partial_path() click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1254
def self._to_partial_path
  @_to_partial_path ||= name.demodulize.underscore.sub!(/_builder$/, '')
end
new(object_name, object, template, options, proc) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1266
def initialize(object_name, object, template, options, proc)
  @nested_child_index = {}
  @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
  @parent_builder = options[:parent_builder]
  @default_options = @options ? @options.slice(:index, :namespace) : {}
  if @object_name.to_s.match(/\[\]$/)
    if object ||= @template.instance_variable_get("@#{Regexp.last_match.pre_match}") and object.respond_to?(:to_param)
      @auto_index = object.to_param
    else
      raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}"
    end
  end
  @multipart = nil
end

Public Instance Methods

button(value=nil, options={}) click to toggle source

Add the submit button for the given form. When no value is given, it checks if the object is a new resource or not to create the proper label:

<%= form_for @post do |f| %>
  <%= f.button %>
<% end %>

In the example above, if @post is a new record, it will use “Create Post” as submit button label, otherwise, it uses “Update Post”.

Those labels can be customized using I18n, under the helpers.submit key and accept the %{model} as translation interpolation:

en:
  helpers:
    button:
      create: "Create a %{model}"
      update: "Confirm changes to %{model}"

It also searches for a key specific for the given object:

en:
  helpers:
    button:
      post:
        create: "Add %{model}"
# File lib/action_view/helpers/form_helper.rb, line 1402
def button(value=nil, options={})
  value, options = nil, value if value.is_a?(Hash)
  value ||= submit_default_value
  @template.button_tag(value, options)
end
check_box(method, options = {}, checked_value = "1", unchecked_value = "0") click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1324
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  @template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value)
end
collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) click to toggle source
# File lib/action_view/helpers/form_options_helper.rb, line 645
def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  @template.collection_select(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options))
end
date_select(method, options = {}, html_options = {}) click to toggle source
# File lib/action_view/helpers/date_helper.rb, line 1049
def date_select(method, options = {}, html_options = {})
  @template.date_select(@object_name, method, objectify_options(options), html_options)
end
datetime_select(method, options = {}, html_options = {}) click to toggle source
# File lib/action_view/helpers/date_helper.rb, line 1057
def datetime_select(method, options = {}, html_options = {})
  @template.datetime_select(@object_name, method, objectify_options(options), html_options)
end
emitted_hidden_id?() click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1408
def emitted_hidden_id?
  @emitted_hidden_id ||= nil
end
fields_for(record_name, record_object = nil, fields_options = {}, &block) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1293
def fields_for(record_name, record_object = nil, fields_options = {}, &block)
  fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
  fields_options[:builder] ||= options[:builder]
  fields_options[:parent_builder] = self
  fields_options[:namespace] = fields_options[:parent_builder].options[:namespace]

  case record_name
  when String, Symbol
    if nested_attributes_association?(record_name)
      return fields_for_with_nested_attributes(record_name, record_object, fields_options, block)
    end
  else
    record_object = record_name.is_a?(Array) ? record_name.last : record_name
    record_name   = ActiveModel::Naming.param_key(record_object)
  end

  index = if options.has_key?(:index)
    "[#{options[:index]}]"
  elsif defined?(@auto_index)
    self.object_name = @object_name.to_s.sub(/\[\]$/,"")
    "[#{@auto_index}]"
  end
  record_name = "#{object_name}#{index}[#{record_name}]"

  @template.fields_for(record_name, record_object, fields_options, &block)
end
file_field(method, options = {}) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1337
def file_field(method, options = {})
  self.multipart = true
  @template.file_field(@object_name, method, objectify_options(options))
end
grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) click to toggle source
# File lib/action_view/helpers/form_options_helper.rb, line 649
def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  @template.grouped_collection_select(@object_name, method, collection, group_method, group_label_method, option_key_method, option_value_method, objectify_options(options), @default_options.merge(html_options))
end
hidden_field(method, options = {}) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1332
def hidden_field(method, options = {})
  @emitted_hidden_id = true if method == :id
  @template.hidden_field(@object_name, method, objectify_options(options))
end
label(method, text = nil, options = {}, &block) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1320
def label(method, text = nil, options = {}, &block)
  @template.label(@object_name, method, text, objectify_options(options), &block)
end
multipart=(multipart) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1249
def multipart=(multipart)
  @multipart = multipart
  parent_builder.multipart = multipart if parent_builder
end
radio_button(method, tag_value, options = {}) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1328
def radio_button(method, tag_value, options = {})
  @template.radio_button(@object_name, method, tag_value, objectify_options(options))
end
select(method, choices, options = {}, html_options = {}) click to toggle source
# File lib/action_view/helpers/form_options_helper.rb, line 641
def select(method, choices, options = {}, html_options = {})
  @template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options))
end
submit(value=nil, options={}) click to toggle source

Add the submit button for the given form. When no value is given, it checks if the object is a new resource or not to create the proper label:

<%= form_for @post do |f| %>
  <%= f.submit %>
<% end %>

In the example above, if @post is a new record, it will use “Create Post” as submit button label, otherwise, it uses “Update Post”.

Those labels can be customized using I18n, under the helpers.submit key and accept the %{model} as translation interpolation:

en:
  helpers:
    submit:
      create: "Create a %{model}"
      update: "Confirm changes to %{model}"

It also searches for a key specific for the given object:

en:
  helpers:
    submit:
      post:
        create: "Add %{model}"
# File lib/action_view/helpers/form_helper.rb, line 1369
def submit(value=nil, options={})
  value, options = nil, value if value.is_a?(Hash)
  value ||= submit_default_value
  @template.submit_tag(value, options)
end
time_select(method, options = {}, html_options = {}) click to toggle source
# File lib/action_view/helpers/date_helper.rb, line 1053
def time_select(method, options = {}, html_options = {})
  @template.time_select(@object_name, method, objectify_options(options), html_options)
end
time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) click to toggle source
# File lib/action_view/helpers/form_options_helper.rb, line 653
def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
  @template.time_zone_select(@object_name, method, priority_zones, objectify_options(options), @default_options.merge(html_options))
end
to_model() click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1262
def to_model
  self
end
to_partial_path() click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1258
def to_partial_path
  self.class._to_partial_path
end

Private Instance Methods

convert_to_model(object) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1475
def convert_to_model(object)
  object.respond_to?(:to_model) ? object.to_model : object
end
fields_for_nested_model(name, object, options, block) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1461
def fields_for_nested_model(name, object, options, block)
  object = convert_to_model(object)

  parent_include_id = self.options.fetch(:include_id, true)
  include_id = options.fetch(:include_id, parent_include_id)
  options[:hidden_field_id] = object.persisted? && include_id
  @template.fields_for(name, object, options, &block)
end
fields_for_with_nested_attributes(association_name, association, options, block) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1439
def fields_for_with_nested_attributes(association_name, association, options, block)
  name = "#{object_name}[#{association_name}_attributes]"
  association = convert_to_model(association)

  if association.respond_to?(:persisted?)
    association = [association] if @object.send(association_name).is_a?(Array)
  elsif !association.respond_to?(:to_ary)
    association = @object.send(association_name)
  end

  if association.respond_to?(:to_ary)
    explicit_child_index = options[:child_index]
    output = ActiveSupport::SafeBuffer.new
    association.each do |child|
      output << fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, options, block)
    end
    output
  elsif association
    fields_for_nested_model(name, association, options, block)
  end
end
nested_attributes_association?(association_name) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1435
def nested_attributes_association?(association_name)
  @object.respond_to?("#{association_name}_attributes=")
end
nested_child_index(name) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1470
def nested_child_index(name)
  @nested_child_index[name] ||= -1
  @nested_child_index[name] += 1
end
objectify_options(options) click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1413
def objectify_options(options)
  @default_options.merge(options.merge(:object => @object))
end
submit_default_value() click to toggle source
# File lib/action_view/helpers/form_helper.rb, line 1417
def submit_default_value
  object = convert_to_model(@object)
  key    = object ? (object.persisted? ? :update : :create) : :submit

  model = if object.class.respond_to?(:model_name)
    object.class.model_name.human
  else
    @object_name.to_s.humanize
  end

  defaults = []
  defaults << :"helpers.submit.#{object_name}.#{key}"
  defaults << :"helpers.submit.#{key}"
  defaults << "#{key.to_s.humanize} #{model}"

  I18n.t(defaults.shift, :model => model, :default => defaults)
end