From 6e7b593992f8f6f8e783b87e7ab9d1d5a97063f7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 14 May 2007 17:30:35 +0000 Subject: Added record identifications to FormHelper#form_for and PrototypeHelper#remote_form_for [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6731 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_view/helpers/form_helper.rb | 61 ++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/helpers/form_helper.rb') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 3af7236b30..d307c194d2 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -103,6 +103,41 @@ module ActionView # The above form will then have the id attribute with the value person_form, which you can then # style with CSS or manipulate with JavaScript. # + # === Relying on record identification + # + # In addition to manually configuring the form_for call, you can also rely on record identification, which will use + # the conventions and named routes of that approach. Examples: + # + # <% form_for(@post) do |f| %> + # ... + # <% end %> + # + # This will expand to be the same as: + # + # <% form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %> + # ... + # <% end %> + # + # And for new records: + # + # <% form_for(Post.new) do |f| %> + # ... + # <% end %> + # + # This will expand to be the same as: + # + # <% form_for :post, @post, :url => posts_path, :html => { :class => "new_post", :id => "new_post" } do |f| %> + # ... + # <% end %> + # + # You can also overwrite the individual conventions, like this: + # + # <% form_for(@post, :url => super_post_path(@post)) do |f| %> + # ... + # <% end %> + # + # === Customized form builders + # # You can also build forms using a customized FormBuilder class. Subclass FormBuilder and override or define some more helpers, # then use your custom builder. For example, let's say you made a helper to automatically add labels to form inputs. # @@ -120,13 +155,37 @@ module ActionView # end # # If you don't need to attach a form to a model instance, then check out FormTagHelper#form_tag. - def form_for(object_name, *args, &proc) + def form_for(record_or_name, *args, &proc) raise ArgumentError, "Missing block" unless block_given? + options = args.last.is_a?(Hash) ? args.pop : {} + + case record_or_name + when String, Symbol + object_name = record_or_name + else + object = record_or_name + object_name = ActionController::RecordIdentifier.singular_class_name(record_or_name) + apply_form_for_options!(object, options) + end + concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {}), proc.binding) fields_for(object_name, *(args << options), &proc) concat('', proc.binding) end + + def apply_form_for_options!(object, options) #:nodoc: + html_options = if object.respond_to?(:new_record?) && object.new_record? + { :class => dom_class(object, :new), :id => dom_id(object), :method => :post } + else + { :class => dom_class(object, :edit), :id => dom_id(object, :edit), :method => :put } + end + + options[:html] ||= {} + options[:html].reverse_merge!(html_options) + + options[:url] ||= polymorphic_path(object, self) + end # Creates a scope around a specific model object like form_for, but doesn't create the form tags themselves. This makes # fields_for suitable for specifying additional model objects in the same form: -- cgit v1.2.3