aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/form_helper.rb
diff options
context:
space:
mode:
authorAlexander Uvarov <alexander.uvarov@gmail.com>2011-02-25 20:19:05 +0500
committerSantiago Pastorino <santiago@wyeworks.com>2011-03-04 16:38:08 -0200
commit0db915efd1240f493c50b7b9f5d1ea5f1e3eec10 (patch)
treefd9aea1aca3a012a9fa8a0f7faa764e9702c3bf4 /actionpack/lib/action_view/helpers/form_helper.rb
parentf00a39845676bd552e31f47f3588ca266d4f010a (diff)
downloadrails-0db915efd1240f493c50b7b9f5d1ea5f1e3eec10.tar.gz
rails-0db915efd1240f493c50b7b9f5d1ea5f1e3eec10.tar.bz2
rails-0db915efd1240f493c50b7b9f5d1ea5f1e3eec10.zip
Add an option to FormBuilder to omit hidden field with id
[#4551 state:committed] Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
Diffstat (limited to 'actionpack/lib/action_view/helpers/form_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb24
1 files changed, 22 insertions, 2 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index befaa3e8d9..669ccd2a2d 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -262,6 +262,24 @@ module ActionView
# ...
# </form>
#
+ # === Removing hidden model id's
+ #
+ # The form_for method automatically includes the model id as a hidden field in the form.
+ # This is used to maintain the correlation between the form data and it's associated model.
+ # Some ORM systems do not use id's on nested models so in this case you want to be able
+ # to disable the hidden id.
+ #
+ # In the following example the Post model has many Comments stored within it in a NoSQL database,
+ # thus there is no primary key for comments.
+ #
+ # Example:
+ #
+ # <%= form(@post) do |f| %>
+ # <% f.fields_for(:comments, :include_id => false) do |cf| %>
+ # ...
+ # <% end %>
+ # <% end %>
+ #
# === Customized form builders
#
# You can also build forms using a customized FormBuilder class. Subclass
@@ -332,7 +350,7 @@ module ActionView
options[:html][:remote] = options.delete(:remote)
options[:html][:authenticity_token] = options.delete(:authenticity_token)
-
+
builder = options[:parent_builder] = instantiate_builder(object_name, object, options, &proc)
fields_for = fields_for(object_name, object, options, &proc)
default_options = builder.multipart? ? { :multipart => true } : {}
@@ -1326,7 +1344,9 @@ module ActionView
def fields_for_nested_model(name, object, options, block)
object = convert_to_model(object)
- options[:hidden_field_id] = object.persisted?
+ parent_include_id = self.options.fetch(:include_id, true)
+ include_id = options.fetch(:include_id, parent_include_id)
+ options[:hidden_field_id] = object.persisted? && include_id
@template.fields_for(name, object, options, &block)
end