Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Includes:
ActiveModelFormBuilder
Defined in:
actionpack/lib/action_view/helpers/form_helper.rb,
actionpack/lib/action_view/helpers/date_helper.rb,
actionpack/lib/action_view/helpers/active_model_helper.rb,
actionpack/lib/action_view/helpers/form_options_helper.rb

Overview

:nodoc:

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (FormBuilder) initialize(object_name, object, template, options, proc)

A new instance of FormBuilder



1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1115

def initialize(object_name, object, template, options, proc)
  @nested_child_index = {}
  @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
  @default_options = @options ? @options.slice(:index) : {}
  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
end

Instance Attribute Details

- (Object) object

Returns the value of attribute object



1105
1106
1107
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1105

def object
  @object
end

- (Object) object_name

Returns the value of attribute object_name



1105
1106
1107
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1105

def object_name
  @object_name
end

- (Object) options

Returns the value of attribute options



1105
1106
1107
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1105

def options
  @options
end

Class Method Details

+ (Object) model_name



1107
1108
1109
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1107

def self.model_name
  @model_name ||= Struct.new(:partial_path).new(name.demodulize.underscore.sub!(/_builder$/, ''))
end

Instance Method Details

- (Object) check_box(method, options = {}, checked_value = "1", unchecked_value = "0")



1179
1180
1181
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1179

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

- (Object) collection_select(method, collection, value_method, text_method, options = {}, html_options = {})



614
615
616
# File 'actionpack/lib/action_view/helpers/form_options_helper.rb', line 614

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

- (Object) date_select(method, options = {}, html_options = {})



968
969
970
# File 'actionpack/lib/action_view/helpers/date_helper.rb', line 968

def date_select(method, options = {}, html_options = {})
  @template.date_select(@object_name, method, objectify_options(options), html_options)
end

- (Object) datetime_select(method, options = {}, html_options = {})



976
977
978
# File 'actionpack/lib/action_view/helpers/date_helper.rb', line 976

def datetime_select(method, options = {}, html_options = {})
  @template.datetime_select(@object_name, method, objectify_options(options), html_options)
end

- (Boolean) emitted_hidden_id?

Returns:

  • (Boolean)


1225
1226
1227
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1225

def emitted_hidden_id?
  @emitted_hidden_id
end

- (Object) fields_for(record_or_name_or_array, *args, &block)



1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1140

def fields_for(record_or_name_or_array, *args, &block)
  if options.has_key?(:index)
    index = "[#{options[:index]}]"
  elsif defined?(@auto_index)
    self.object_name = @object_name.to_s.sub(/\[\]$/,"")
    index = "[#{@auto_index}]"
  else
    index = ""
  end

  if options[:builder]
    args << {} unless args.last.is_a?(Hash)
    args.last[:builder] ||= options[:builder]
  end

  case record_or_name_or_array
  when String, Symbol
    if nested_attributes_association?(record_or_name_or_array)
      return fields_for_with_nested_attributes(record_or_name_or_array, args, block)
    else
      name = "#{object_name}#{index}[#{record_or_name_or_array}]"
    end
  when Array
    object = record_or_name_or_array.last
    name = "#{object_name}#{index}[#{ActiveModel::Naming.singular(object)}]"
    args.unshift(object)
  else
    object = record_or_name_or_array
    name = "#{object_name}#{index}[#{ActiveModel::Naming.singular(object)}]"
    args.unshift(object)
  end

  @template.fields_for(name, *args, &block)
end

- (Object) grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})



618
619
620
# File 'actionpack/lib/action_view/helpers/form_options_helper.rb', line 618

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

- (Object) hidden_field(method, options = {})



1187
1188
1189
1190
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1187

def hidden_field(method, options = {})
  @emitted_hidden_id = true if method == :id
  @template.hidden_field(@object_name, method, objectify_options(options))
end

- (Object) label(method, text = nil, options = {}, &block)



1175
1176
1177
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1175

def label(method, text = nil, options = {}, &block)
  @template.label(@object_name, method, text, objectify_options(options), &block)
end

- (Object) radio_button(method, tag_value, options = {})



1183
1184
1185
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1183

def radio_button(method, tag_value, options = {})
  @template.radio_button(@object_name, method, tag_value, objectify_options(options))
end

- (Object) select(method, choices, options = {}, html_options = {})



610
611
612
# File 'actionpack/lib/action_view/helpers/form_options_helper.rb', line 610

def select(method, choices, options = {}, html_options = {})
  @template.select(@object_name, method, choices, objectify_options(options), @default_options.merge(html_options))
end

- (Object) submit(value = nil, options = {})

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}"


1219
1220
1221
1222
1223
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1219

def submit(value=nil, options={})
  value, options = nil, value if value.is_a?(Hash)
  value ||= submit_default_value
  @template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit"))
end

- (Object) time_select(method, options = {}, html_options = {})



972
973
974
# File 'actionpack/lib/action_view/helpers/date_helper.rb', line 972

def time_select(method, options = {}, html_options = {})
  @template.time_select(@object_name, method, objectify_options(options), html_options)
end

- (Object) time_zone_select(method, priority_zones = nil, options = {}, html_options = {})



622
623
624
# File 'actionpack/lib/action_view/helpers/form_options_helper.rb', line 622

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

- (Object) to_model



1111
1112
1113
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1111

def to_model
  self
end