Class: ActionView::Helpers::FormBuilder
- Inherits:
-
Object
- Object
- ActionView::Helpers::FormBuilder
- Includes:
- ActiveModelFormBuilder
- Defined in:
- lib/action_view/helpers/date_helper.rb,
lib/action_view/helpers/form_helper.rb,
lib/action_view/helpers/active_model_helper.rb,
lib/action_view/helpers/form_options_helper.rb
Instance Attribute Summary collapse
-
#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.
Class Method Summary collapse
Instance Method Summary collapse
- #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_or_name_or_array, *args, &block) ⇒ 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
Constructor Details
#initialize(object_name, object, template, options, proc) ⇒ FormBuilder
Returns a new instance of FormBuilder.
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 |
# File 'lib/action_view/helpers/form_helper.rb', line 1115 def initialize(object_name, object, template, , proc) @nested_child_index = {} @object_name, @object, @template, @options, @proc = object_name, object, template, , 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 'lib/action_view/helpers/form_helper.rb', line 1105 def object @object end |
#object_name ⇒ Object
Returns the value of attribute object_name.
1105 1106 1107 |
# File 'lib/action_view/helpers/form_helper.rb', line 1105 def object_name @object_name end |
#options ⇒ Object
Returns the value of attribute options.
1105 1106 1107 |
# File 'lib/action_view/helpers/form_helper.rb', line 1105 def @options end |
Class Method Details
.model_name ⇒ Object
1107 1108 1109 |
# File '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
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
1179 1180 1181 |
# File 'lib/action_view/helpers/form_helper.rb', line 1179 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
614 615 616 |
# File 'lib/action_view/helpers/form_options_helper.rb', line 614 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
968 969 970 |
# File 'lib/action_view/helpers/date_helper.rb', line 968 def date_select(method, = {}, = {}) @template.date_select(@object_name, method, (), ) end |
#datetime_select(method, options = {}, html_options = {}) ⇒ Object
976 977 978 |
# File 'lib/action_view/helpers/date_helper.rb', line 976 def datetime_select(method, = {}, = {}) @template.datetime_select(@object_name, method, (), ) end |
#emitted_hidden_id? ⇒ Boolean
1225 1226 1227 |
# File 'lib/action_view/helpers/form_helper.rb', line 1225 def emitted_hidden_id? @emitted_hidden_id end |
#fields_for(record_or_name_or_array, *args, &block) ⇒ Object
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 'lib/action_view/helpers/form_helper.rb', line 1140 def fields_for(record_or_name_or_array, *args, &block) if .has_key?(:index) index = "[#{[:index]}]" elsif defined?(@auto_index) self.object_name = @object_name.to_s.sub(/\[\]$/,"") index = "[#{@auto_index}]" else index = "" end if [:builder] args << {} unless args.last.is_a?(Hash) args.last[:builder] ||= [: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 |
#grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object
618 619 620 |
# File '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, = {}, = {}) @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
1187 1188 1189 1190 |
# File 'lib/action_view/helpers/form_helper.rb', line 1187 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
1175 1176 1177 |
# File 'lib/action_view/helpers/form_helper.rb', line 1175 def label(method, text = nil, = {}, &block) @template.label(@object_name, method, text, (), &block) end |
#radio_button(method, tag_value, options = {}) ⇒ Object
1183 1184 1185 |
# File 'lib/action_view/helpers/form_helper.rb', line 1183 def (method, tag_value, = {}) @template.(@object_name, method, tag_value, ()) end |
#select(method, choices, options = {}, html_options = {}) ⇒ Object
610 611 612 |
# File 'lib/action_view/helpers/form_options_helper.rb', line 610 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}"
1219 1220 1221 1222 1223 |
# File 'lib/action_view/helpers/form_helper.rb', line 1219 def submit(value=nil, ={}) value, = nil, value if value.is_a?(Hash) value ||= submit_default_value @template.submit_tag(value, .reverse_merge(:id => "#{object_name}_submit")) end |
#time_select(method, options = {}, html_options = {}) ⇒ Object
972 973 974 |
# File 'lib/action_view/helpers/date_helper.rb', line 972 def time_select(method, = {}, = {}) @template.time_select(@object_name, method, (), ) end |
#time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object
622 623 624 |
# File 'lib/action_view/helpers/form_options_helper.rb', line 622 def time_zone_select(method, priority_zones = nil, = {}, = {}) @template.time_zone_select(@object_name, method, priority_zones, (), @default_options.merge()) end |
#to_model ⇒ Object
1111 1112 1113 |
# File 'lib/action_view/helpers/form_helper.rb', line 1111 def to_model self end |