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)

Returns a new instance of FormBuilder



1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1112

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



1102
1103
1104
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1102

def object
  @object
end

- (Object) object_name

Returns the value of attribute object_name



1102
1103
1104
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1102

def object_name
  @object_name
end

- (Object) options

Returns the value of attribute options



1102
1103
1104
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1102

def options
  @options
end

Class Method Details

+ (Object) model_name



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

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



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

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 = {})



966
967
968
# File 'actionpack/lib/action_view/helpers/date_helper.rb', line 966

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 = {})



974
975
976
# File 'actionpack/lib/action_view/helpers/date_helper.rb', line 974

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)


1222
1223
1224
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1222

def emitted_hidden_id?
  @emitted_hidden_id
end

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



1137
1138
1139
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
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1137

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 = {})



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

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)



1172
1173
1174
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1172

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 = {})



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

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


1216
1217
1218
1219
1220
# File 'actionpack/lib/action_view/helpers/form_helper.rb', line 1216

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 = {})



970
971
972
# File 'actionpack/lib/action_view/helpers/date_helper.rb', line 970

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



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

def to_model
  self
end