Class: ActionView::Helpers::FormBuilder
- Inherits:
-
Object
- Object
- ActionView::Helpers::FormBuilder
- Defined in:
- lib/action_view/helpers/date_helper.rb,
lib/action_view/helpers/form_helper.rb,
lib/action_view/helpers/form_options_helper.rb
Instance Attribute Summary collapse
-
#multipart ⇒ Object
(also: #multipart?)
Returns the value of attribute multipart.
-
#object ⇒ Object
Returns the value of attribute object.
-
#object_name ⇒ Object
Returns the value of attribute object_name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parent_builder ⇒ Object
readonly
Returns the value of attribute parent_builder.
Class Method Summary collapse
Instance Method Summary collapse
-
#button(value = nil, options = {}) ⇒ Object
Add the submit button for the given form.
- #check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
- #collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
- #date_select(method, options = {}, html_options = {}) ⇒ Object
- #datetime_select(method, options = {}, html_options = {}) ⇒ Object
- #emitted_hidden_id? ⇒ Boolean
- #fields_for(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object
- #file_field(method, options = {}) ⇒ Object
- #grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object
- #hidden_field(method, options = {}) ⇒ Object
-
#initialize(object_name, object, template, options, proc) ⇒ FormBuilder
constructor
A new instance of FormBuilder.
- #label(method, text = nil, options = {}, &block) ⇒ Object
- #radio_button(method, tag_value, options = {}) ⇒ Object
- #select(method, choices, options = {}, html_options = {}) ⇒ Object
-
#submit(value = nil, options = {}) ⇒ Object
Add the submit button for the given form.
- #time_select(method, options = {}, html_options = {}) ⇒ Object
- #time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object
- #to_model ⇒ Object
- #to_partial_path ⇒ Object
Constructor Details
#initialize(object_name, object, template, options, proc) ⇒ FormBuilder
Returns a new instance of FormBuilder.
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 |
# File 'lib/action_view/helpers/form_helper.rb', line 1259 def initialize(object_name, object, template, , proc) @nested_child_index = {} @object_name, @object, @template, @options, @proc = object_name, object, template, , proc @parent_builder = [: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 |
Instance Attribute Details
#multipart ⇒ Object Also known as: multipart?
Returns the value of attribute multipart.
1239 1240 1241 |
# File 'lib/action_view/helpers/form_helper.rb', line 1239 def multipart @multipart end |
#object ⇒ Object
Returns the value of attribute object.
1237 1238 1239 |
# File 'lib/action_view/helpers/form_helper.rb', line 1237 def object @object end |
#object_name ⇒ Object
Returns the value of attribute object_name.
1237 1238 1239 |
# File 'lib/action_view/helpers/form_helper.rb', line 1237 def object_name @object_name end |
#options ⇒ Object
Returns the value of attribute options.
1237 1238 1239 |
# File 'lib/action_view/helpers/form_helper.rb', line 1237 def @options end |
#parent_builder ⇒ Object (readonly)
Returns the value of attribute parent_builder.
1239 1240 1241 |
# File 'lib/action_view/helpers/form_helper.rb', line 1239 def parent_builder @parent_builder end |
Class Method Details
._to_partial_path ⇒ Object
1247 1248 1249 |
# File 'lib/action_view/helpers/form_helper.rb', line 1247 def self._to_partial_path @_to_partial_path ||= name.demodulize.underscore.sub!(/_builder$/, '') end |
Instance Method Details
#button(value = nil, options = {}) ⇒ Object
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}"
1395 1396 1397 1398 1399 |
# File 'lib/action_view/helpers/form_helper.rb', line 1395 def (value=nil, ={}) value, = nil, value if value.is_a?(Hash) value ||= submit_default_value @template.(value, ) end |
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
1317 1318 1319 |
# File 'lib/action_view/helpers/form_helper.rb', line 1317 def check_box(method, = {}, checked_value = "1", unchecked_value = "0") @template.check_box(@object_name, method, (), checked_value, unchecked_value) end |
#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
645 646 647 |
# File 'lib/action_view/helpers/form_options_helper.rb', line 645 def collection_select(method, collection, value_method, text_method, = {}, = {}) @template.collection_select(@object_name, method, collection, value_method, text_method, (), @default_options.merge()) end |
#date_select(method, options = {}, html_options = {}) ⇒ Object
1041 1042 1043 |
# File 'lib/action_view/helpers/date_helper.rb', line 1041 def date_select(method, = {}, = {}) @template.date_select(@object_name, method, (), ) end |
#datetime_select(method, options = {}, html_options = {}) ⇒ Object
1049 1050 1051 |
# File 'lib/action_view/helpers/date_helper.rb', line 1049 def datetime_select(method, = {}, = {}) @template.datetime_select(@object_name, method, (), ) end |
#emitted_hidden_id? ⇒ Boolean
1401 1402 1403 |
# File 'lib/action_view/helpers/form_helper.rb', line 1401 def emitted_hidden_id? @emitted_hidden_id ||= nil end |
#fields_for(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 |
# File 'lib/action_view/helpers/form_helper.rb', line 1286 def fields_for(record_name, record_object = nil, = {}, &block) , record_object = record_object, nil if record_object.is_a?(Hash) && record_object. [:builder] ||= [:builder] [:parent_builder] = self [:namespace] = [:parent_builder].[:namespace] case record_name when String, Symbol if nested_attributes_association?(record_name) return fields_for_with_nested_attributes(record_name, record_object, , 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 .has_key?(:index) "[#{[: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, , &block) end |
#file_field(method, options = {}) ⇒ Object
1330 1331 1332 1333 |
# File 'lib/action_view/helpers/form_helper.rb', line 1330 def file_field(method, = {}) self.multipart = true @template.file_field(@object_name, method, ()) end |
#grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object
649 650 651 |
# 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, = {}, = {}) @template.grouped_collection_select(@object_name, method, collection, group_method, group_label_method, option_key_method, option_value_method, (), @default_options.merge()) end |
#hidden_field(method, options = {}) ⇒ Object
1325 1326 1327 1328 |
# File 'lib/action_view/helpers/form_helper.rb', line 1325 def hidden_field(method, = {}) @emitted_hidden_id = true if method == :id @template.hidden_field(@object_name, method, ()) end |
#label(method, text = nil, options = {}, &block) ⇒ Object
1313 1314 1315 |
# File 'lib/action_view/helpers/form_helper.rb', line 1313 def label(method, text = nil, = {}, &block) @template.label(@object_name, method, text, (), &block) end |
#radio_button(method, tag_value, options = {}) ⇒ Object
1321 1322 1323 |
# File 'lib/action_view/helpers/form_helper.rb', line 1321 def (method, tag_value, = {}) @template.(@object_name, method, tag_value, ()) end |
#select(method, choices, options = {}, html_options = {}) ⇒ Object
641 642 643 |
# File 'lib/action_view/helpers/form_options_helper.rb', line 641 def select(method, choices, = {}, = {}) @template.select(@object_name, method, choices, (), @default_options.merge()) end |
#submit(value = nil, options = {}) ⇒ Object
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}"
1362 1363 1364 1365 1366 |
# File 'lib/action_view/helpers/form_helper.rb', line 1362 def submit(value=nil, ={}) value, = nil, value if value.is_a?(Hash) value ||= submit_default_value @template.submit_tag(value, ) end |
#time_select(method, options = {}, html_options = {}) ⇒ Object
1045 1046 1047 |
# File 'lib/action_view/helpers/date_helper.rb', line 1045 def time_select(method, = {}, = {}) @template.time_select(@object_name, method, (), ) end |
#time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object
653 654 655 |
# File 'lib/action_view/helpers/form_options_helper.rb', line 653 def time_zone_select(method, priority_zones = nil, = {}, = {}) @template.time_zone_select(@object_name, method, priority_zones, (), @default_options.merge()) end |
#to_model ⇒ Object
1255 1256 1257 |
# File 'lib/action_view/helpers/form_helper.rb', line 1255 def to_model self end |
#to_partial_path ⇒ Object
1251 1252 1253 |
# File 'lib/action_view/helpers/form_helper.rb', line 1251 def to_partial_path self.class._to_partial_path end |