aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/form_helper.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-10 23:57:36 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-10 23:57:36 +0000
commit2594f2abe9144d1a50c8c7404627817ebd0386f0 (patch)
tree4af0cd36171f8849218d2471315ef51f68241092 /actionpack/lib/action_view/helpers/form_helper.rb
parent445f89ccb57040e42355e2078d13f4b70a95ee7c (diff)
downloadrails-2594f2abe9144d1a50c8c7404627817ebd0386f0.tar.gz
rails-2594f2abe9144d1a50c8c7404627817ebd0386f0.tar.bz2
rails-2594f2abe9144d1a50c8c7404627817ebd0386f0.zip
Added that form helpers now take an index option #448 [Tim Bates]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@378 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/helpers/form_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb27
1 files changed, 25 insertions, 2 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 105f464fc8..bed9eae244 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -45,6 +45,15 @@ module ActionView
# <input type="submit" value="Save">
# </form>
#
+ # If the helper is being used to generate a repetitive sequence of similar form elements, for example in a partial
+ # used by render_collection_of_partials, the "index" option may come in handy. Example:
+ #
+ # <%= text_field "person", "name", "index" => 1 %>
+ #
+ # becomes
+ #
+ # <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" />
+ #
# There's also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html,
# link:classes/ActionView/Helpers/DateHelper.html, and link:classes/ActionView/Helpers/ActiveRecordHelper.html
module FormHelper
@@ -201,17 +210,31 @@ module ActionView
private
def add_default_name_and_id(options)
- options['name'] = tag_name unless options.has_key? "name"
- options['id'] = tag_id unless options.has_key? "id"
+ if options.has_key? "index"
+ options['name'] = tag_name_with_index(options["index"]) unless options.has_key? "name"
+ options['id'] = tag_id_with_index(options["index"]) unless options.has_key? "id"
+ options.delete("index")
+ else
+ options['name'] = tag_name unless options.has_key? "name"
+ options['id'] = tag_id unless options.has_key? "id"
+ end
end
def tag_name
"#{@object_name}[#{@method_name}]"
end
+
+ def tag_name_with_index(index)
+ "#{@object_name}[#{index}][#{@method_name}]"
+ end
def tag_id
"#{@object_name}_#{@method_name}"
end
+
+ def tag_id_with_index(index)
+ "#{@object_name}_#{index}_#{@method_name}"
+ end
end
end
end