Permalink
Browse files

Added :format option for form_for helper and spec for this [#5226 sta…

…te:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent c25c81e commit 3b6d7f0df881d00d94314b0e3b6f328dbd04d1e6 @chrome chrome committed with josevalim Jul 28, 2010
Showing with 27 additions and 3 deletions.
  1. +9 −1 actionpack/lib/action_view/helpers/form_helper.rb
  2. +18 −2 actionpack/test/template/form_helper_test.rb
View
10 actionpack/lib/action_view/helpers/form_helper.rb
@@ -202,6 +202,12 @@ module FormHelper
# ...
# <% end %>
#
+ # You can also set the answer format, like this:
+ #
+ # <%= form_for(@post, :format => :json) do |f| %>
+ # ...
+ # <% end %>
+ #
# If you have an object that needs to be represented as a different
# parameter, like a Client that acts as a Person:
#
@@ -332,7 +338,9 @@ def apply_form_for_options!(object_or_array, options) #:nodoc:
options[:html] ||= {}
options[:html].reverse_merge!(html_options)
- options[:url] ||= polymorphic_path(object_or_array)
+ options[:url] ||= options[:format] ? \
+ polymorphic_path(object_or_array, :format => options.delete(:format)) : \
+ polymorphic_path(object_or_array)
end
# Creates a scope around a specific model object like form_for, but
View
20 actionpack/test/template/form_helper_test.rb
@@ -638,6 +638,18 @@ def test_form_for
assert_dom_equal expected, output_buffer
end
+ def test_form_for_with_format
+ form_for(@post, :format => :json, :html => { :id => "edit_post_123", :class => "edit_post" }) do |f|
+ concat f.label(:title)
+ end
+
+ expected = whole_form("/posts/123.json", "edit_post_123" , "edit_post", :method => "put") do
+ "<label for='post_title'>Title</label>"
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
def test_form_for_with_symbol_object_name
form_for(@post, :as => "other_name", :html => { :id => 'create-post' }) do |f|
concat f.label(:title, :class => 'post_title')
@@ -1758,8 +1770,12 @@ def posts_path
"/posts"
end
- def post_path(post)
- "/posts/#{post.id}"
+ def post_path(post, options = {})
+ if options[:format]
+ "/posts/#{post.id}.#{options[:format]}"
+ else
+ "/posts/#{post.id}"
+ end
end
def protect_against_forgery?

0 comments on commit 3b6d7f0

Please sign in to comment.