提交 139e2c4a 编写于 作者: M Marcel Molina

Change form_for and fields_for method signatures to take object name and...

Change form_for and fields_for method signatures to take object name and object as separate arguments rather than as a Hash.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3201 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 833d9c89
*SVN*
* Change form_for and fields_for method signatures to take object name and object as separate arguments rather than as a Hash. [DHH]
* Introduce :selected option to the select helper. Allows you to specify a selection other than the current value of object.method. Specify :selected => nil to leave all options unselected. #2991 [Jonathan Viney <jonathan@bluewire.net.nz>]
* Initialize @optional in routing code to avoid warnings about uninitialized access to an instance variable. [Nicholas Seckar]
......
......@@ -83,12 +83,12 @@ module FormHelper
# That in itself is a modest increase in comfort. The big news is that form_for allows us to more easily escape the instance
# variable convention, so while the stand-alone approach would require <tt>text_field :person, :name, :object => person</tt>
# to work with local variables instead of instance ones, the form_for calls remain the same. You simply declare once with
# <tt>:person => person</tt> and all subsequent field calls save <tt>:person</tt> and <tt>:object => person</tt>.
# <tt>:person, person</tt> and all subsequent field calls save <tt>:person</tt> and <tt>:object => person</tt>.
#
# Also note that form_for doesn't create an exclusive scope. It's still possible to use both the stand-alone FormHelper methods
# and methods from FormTagHelper. Example:
#
# <% form_for :person => @person, :url => { :action => "update" } do |f| %>
# <% form_for :person, @person, :url => { :action => "update" } do |f| %>
# First name: <%= f.text_field :first_name %>
# Last name : <%= f.text_field :last_name %>
# Biography : <%= text_area :person, :biography %>
......@@ -97,43 +97,30 @@ module FormHelper
#
# Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base.
# Like collection_select and datetime_select.
def form_for(options, *parameters_for_url, &proc)
keys = [ :url, :method, :multipart ]
leftover_keys = (options.keys - keys)
case leftover_keys.length
when 0 then raise 'No object given!'
when 1 then
object_name = leftover_keys.first
object = options[object_name]
else
raise "Too many options: #{options.inspect}"
end
url_for_options = options[:url]
def form_for(object_name, object, options = {}, &proc)
url_for_options = options[:url]
additional_options = options.reject { |k, v| ![ :method, :multipart ].include?(k) }
concat(form_tag(url_for_options, additional_options, *parameters_for_url), proc.binding)
fields_for({ object_name => object }, &proc)
concat(form_tag(url_for_options, additional_options), proc.binding)
fields_for(object_name, object, &proc)
concat(end_form_tag, proc.binding)
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. Example:
#
# <% form_for :person => @person, :url => { :action => "update" } do |person_form| %>
# <% form_for :person, @person, :url => { :action => "update" } do |person_form| %>
# First name: <%= person_form.text_field :first_name %>
# Last name : <%= person_form.text_field :last_name %>
#
# <% fields_for :permission => @person.permission do |permission_fields| %>
# <% fields_for :permission, @person.permission do |permission_fields| %>
# Admin? : <%= permission_fields.check_box :admin %>
# <% end %>
# <% end %>
#
# Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base.
# Like collection_select and datetime_select.
def fields_for(object = {}, &proc)
form_builder = FormBuilder.new(object.keys.first, object.values.first, self, proc)
def fields_for(object_name, object, &proc)
form_builder = FormBuilder.new(object_name, object, self, proc)
proc.call(form_builder)
end
......
......@@ -507,7 +507,7 @@ def test_date_select_within_fields_for
_erbout = ''
fields_for :post => @post do |f|
fields_for :post, @post do |f|
_erbout.concat f.date_select(:written_on)
end
......@@ -524,7 +524,7 @@ def test_datetime_select_within_fields_for
_erbout = ''
fields_for :post => @post do |f|
fields_for :post, @post do |f|
_erbout.concat f.datetime_select(:updated_at)
end
......
......@@ -199,7 +199,7 @@ def test_auto_index
def test_form_for
_erbout = ''
form_for(:post => @post) do |f|
form_for(:post, @post) do |f|
_erbout.concat f.text_field(:title)
_erbout.concat f.text_area(:body)
_erbout.concat f.check_box(:secret)
......@@ -219,7 +219,7 @@ def test_form_for
def test_fields_for
_erbout = ''
fields_for(:post => @post) do |f|
fields_for(:post, @post) do |f|
_erbout.concat f.text_field(:title)
_erbout.concat f.text_area(:body)
_erbout.concat f.check_box(:secret)
......@@ -237,11 +237,11 @@ def test_fields_for
def test_form_for_and_fields_for
_erbout = ''
form_for(:post => @post) do |post_form|
form_for(:post, @post) do |post_form|
_erbout.concat post_form.text_field(:title)
_erbout.concat post_form.text_area(:body)
fields_for(:parent_post => @post) do |parent_fields|
fields_for(:parent_post, @post) do |parent_fields|
_erbout.concat parent_fields.check_box(:secret)
end
end
......@@ -256,4 +256,4 @@ def test_form_for_and_fields_for
assert_dom_equal expected, _erbout
end
end
\ No newline at end of file
end
......@@ -229,7 +229,7 @@ def test_select_under_fields_for
_erbout = ''
fields_for :post => @post do |f|
fields_for :post, @post do |f|
_erbout.concat f.select(:category, %w( abe <mus> hest))
end
......@@ -330,7 +330,7 @@ def test_collection_select_under_fields_for
_erbout = ''
fields_for :post => @post do |f|
fields_for :post, @post do |f|
_erbout.concat f.collection_select(:author_name, @posts, :author_name, :author_name)
end
......@@ -386,7 +386,7 @@ def test_time_zone_select_under_fields_for
_erbout = ''
fields_for :firm => @firm do |f|
fields_for :firm, @firm do |f|
_erbout.concat f.time_zone_select(:time_zone)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册