Skip to content

Commit

Permalink
Added :format option for form_for helper and spec for this [#5226 sta…
Browse files Browse the repository at this point in the history
…te:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
chrome authored and josevalim committed Sep 1, 2010
1 parent c25c81e commit 3b6d7f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
10 changes: 9 additions & 1 deletion actionpack/lib/action_view/helpers/form_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ module FormHelper
# ... # ...
# <% end %> # <% 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 # If you have an object that needs to be represented as a different
# parameter, like a Client that acts as a Person: # parameter, like a Client that acts as a Person:
# #
Expand Down Expand Up @@ -332,7 +338,9 @@ def apply_form_for_options!(object_or_array, options) #:nodoc:


options[:html] ||= {} options[:html] ||= {}
options[:html].reverse_merge!(html_options) 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 end


# Creates a scope around a specific model object like form_for, but # Creates a scope around a specific model object like form_for, but
Expand Down
20 changes: 18 additions & 2 deletions actionpack/test/template/form_helper_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -638,6 +638,18 @@ def test_form_for
assert_dom_equal expected, output_buffer assert_dom_equal expected, output_buffer
end 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 def test_form_for_with_symbol_object_name
form_for(@post, :as => "other_name", :html => { :id => 'create-post' }) do |f| form_for(@post, :as => "other_name", :html => { :id => 'create-post' }) do |f|
concat f.label(:title, :class => 'post_title') concat f.label(:title, :class => 'post_title')
Expand Down Expand Up @@ -1758,8 +1770,12 @@ def posts_path
"/posts" "/posts"
end end


def post_path(post) def post_path(post, options = {})
"/posts/#{post.id}" if options[:format]
"/posts/#{post.id}.#{options[:format]}"
else
"/posts/#{post.id}"
end
end end


def protect_against_forgery? def protect_against_forgery?
Expand Down

0 comments on commit 3b6d7f0

Please sign in to comment.