Parent

Class Index [+]

Quicksearch

ActionView::Helpers::FormBuilder

Attributes

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

Public Class Methods

_to_partial_path() click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1254
1254:       def self._to_partial_path
1255:         @_to_partial_path ||= name.demodulize.underscore.sub!(/_builder$/, '')
1256:       end
new(object_name, object, template, options, proc) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1266
1266:       def initialize(object_name, object, template, options, proc)
1267:         @nested_child_index = {}
1268:         @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
1269:         @parent_builder = options[:parent_builder]
1270:         @default_options = @options ? @options.slice(:index, :namespace) : {}
1271:         if @object_name.to_s.match(/\[\]$/)
1272:           if object ||= @template.instance_variable_get("@#{Regexp.last_match.pre_match}") and object.respond_to?(:to_param)
1273:             @auto_index = object.to_param
1274:           else
1275:             raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}"
1276:           end
1277:         end
1278:         @multipart = nil
1279:       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
1402:       def button(value=nil, options={})
1403:         value, options = nil, value if value.is_a?(Hash)
1404:         value ||= submit_default_value
1405:         @template.button_tag(value, options)
1406:       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
1324:       def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
1325:         @template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value)
1326:       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
645:       def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
646:         @template.collection_select(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options))
647:       end
date_select(method, options = {}, html_options = {}) click to toggle source
      # File lib/action_view/helpers/date_helper.rb, line 1041
1041:       def date_select(method, options = {}, html_options = {})
1042:         @template.date_select(@object_name, method, objectify_options(options), html_options)
1043:       end
datetime_select(method, options = {}, html_options = {}) click to toggle source
      # File lib/action_view/helpers/date_helper.rb, line 1049
1049:       def datetime_select(method, options = {}, html_options = {})
1050:         @template.datetime_select(@object_name, method, objectify_options(options), html_options)
1051:       end
emitted_hidden_id?() click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1408
1408:       def emitted_hidden_id?
1409:         @emitted_hidden_id ||= nil
1410:       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
1293:       def fields_for(record_name, record_object = nil, fields_options = {}, &block)
1294:         fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
1295:         fields_options[:builder] ||= options[:builder]
1296:         fields_options[:parent_builder] = self
1297:         fields_options[:namespace] = fields_options[:parent_builder].options[:namespace]
1298: 
1299:         case record_name
1300:         when String, Symbol
1301:           if nested_attributes_association?(record_name)
1302:             return fields_for_with_nested_attributes(record_name, record_object, fields_options, block)
1303:           end
1304:         else
1305:           record_object = record_name.is_a?(Array) ? record_name.last : record_name
1306:           record_name   = ActiveModel::Naming.param_key(record_object)
1307:         end
1308: 
1309:         index = if options.has_key?(:index)
1310:           "[#{options[:index]}]"
1311:         elsif defined?(@auto_index)
1312:           self.object_name = @object_name.to_s.sub(/\[\]$/,"")
1313:           "[#{@auto_index}]"
1314:         end
1315:         record_name = "#{object_name}#{index}[#{record_name}]"
1316: 
1317:         @template.fields_for(record_name, record_object, fields_options, &block)
1318:       end
file_field(method, options = {}) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1337
1337:       def file_field(method, options = {})
1338:         self.multipart = true
1339:         @template.file_field(@object_name, method, objectify_options(options))
1340:       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
649:       def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
650:         @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))
651:       end
hidden_field(method, options = {}) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1332
1332:       def hidden_field(method, options = {})
1333:         @emitted_hidden_id = true if method == :id
1334:         @template.hidden_field(@object_name, method, objectify_options(options))
1335:       end
label(method, text = nil, options = {}, &block) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1320
1320:       def label(method, text = nil, options = {}, &block)
1321:         @template.label(@object_name, method, text, objectify_options(options), &block)
1322:       end
multipart=(multipart) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1249
1249:       def multipart=(multipart)
1250:         @multipart = multipart
1251:         parent_builder.multipart = multipart if parent_builder
1252:       end
radio_button(method, tag_value, options = {}) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1328
1328:       def radio_button(method, tag_value, options = {})
1329:         @template.radio_button(@object_name, method, tag_value, objectify_options(options))
1330:       end
select(method, choices, options = {}, html_options = {}) click to toggle source
     # File lib/action_view/helpers/form_options_helper.rb, line 641
641:       def select(method, choices, options = {}, html_options = {})
642:         @template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options))
643:       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
1369:       def submit(value=nil, options={})
1370:         value, options = nil, value if value.is_a?(Hash)
1371:         value ||= submit_default_value
1372:         @template.submit_tag(value, options)
1373:       end
time_select(method, options = {}, html_options = {}) click to toggle source
      # File lib/action_view/helpers/date_helper.rb, line 1045
1045:       def time_select(method, options = {}, html_options = {})
1046:         @template.time_select(@object_name, method, objectify_options(options), html_options)
1047:       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
653:       def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
654:         @template.time_zone_select(@object_name, method, priority_zones, objectify_options(options), @default_options.merge(html_options))
655:       end
to_model() click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1262
1262:       def to_model
1263:         self
1264:       end
to_partial_path() click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1258
1258:       def to_partial_path
1259:         self.class._to_partial_path
1260:       end

Private Instance Methods

convert_to_model(object) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1475
1475:         def convert_to_model(object)
1476:           object.respond_to?(:to_model) ? object.to_model : object
1477:         end
fields_for_nested_model(name, object, options, block) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1461
1461:         def fields_for_nested_model(name, object, options, block)
1462:           object = convert_to_model(object)
1463: 
1464:           parent_include_id = self.options.fetch(:include_id, true)
1465:           include_id = options.fetch(:include_id, parent_include_id)
1466:           options[:hidden_field_id] = object.persisted? && include_id
1467:           @template.fields_for(name, object, options, &block)
1468:         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
1439:         def fields_for_with_nested_attributes(association_name, association, options, block)
1440:           name = "#{object_name}[#{association_name}_attributes]"
1441:           association = convert_to_model(association)
1442: 
1443:           if association.respond_to?(:persisted?)
1444:             association = [association] if @object.send(association_name).is_a?(Array)
1445:           elsif !association.respond_to?(:to_ary)
1446:             association = @object.send(association_name)
1447:           end
1448: 
1449:           if association.respond_to?(:to_ary)
1450:             explicit_child_index = options[:child_index]
1451:             output = ActiveSupport::SafeBuffer.new
1452:             association.each do |child|
1453:               output << fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, options, block)
1454:             end
1455:             output
1456:           elsif association
1457:             fields_for_nested_model(name, association, options, block)
1458:           end
1459:         end
nested_attributes_association?(association_name) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1435
1435:         def nested_attributes_association?(association_name)
1436:           @object.respond_to?("#{association_name}_attributes=")
1437:         end
nested_child_index(name) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1470
1470:         def nested_child_index(name)
1471:           @nested_child_index[name] ||= 1
1472:           @nested_child_index[name] += 1
1473:         end
objectify_options(options) click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1413
1413:         def objectify_options(options)
1414:           @default_options.merge(options.merge(:object => @object))
1415:         end
submit_default_value() click to toggle source
      # File lib/action_view/helpers/form_helper.rb, line 1417
1417:         def submit_default_value
1418:           object = convert_to_model(@object)
1419:           key    = object ? (object.persisted? ? :update : :create) : :submit
1420: 
1421:           model = if object.class.respond_to?(:model_name)
1422:             object.class.model_name.human
1423:           else
1424:             @object_name.to_s.humanize
1425:           end
1426: 
1427:           defaults = []
1428:           defaults << :"helpers.submit.#{object_name}.#{key}"
1429:           defaults << :"helpers.submit.#{key}"
1430:           defaults << "#{key.to_s.humanize} #{model}"
1431: 
1432:           I18n.t(defaults.shift, :model => model, :default => defaults)
1433:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.