Permalink
Browse files

text_area should handle nil value option like text_field

  • Loading branch information...
1 parent 3a4d0b1 commit 1424873948a98ab30ac4b5eed35d3de59a54b92a @joelcogen joelcogen committed Jul 23, 2013
View
@@ -1,3 +1,17 @@
+* Fix `text_area` to behave like `text_field` when `nil` is given as
+ value.
+
+ Before:
+
+ f.text_field :field, value: nil #=> <input value="">
+ f.text_area :field, value: nil #=> <textarea>value of field</textarea>
+
+ After:
+
+ f.text_area :field, value: nil #=> <textarea></textarea>
+
+ *Joel Cogen*
+
* Element of the `grouped_options_for_select` can
optionally contain html attributes as the last element of the array.
@@ -10,7 +10,7 @@ def render
options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
end
- content_tag("textarea", options.delete('value') || value_before_type_cast(object), options)
+ content_tag("textarea", options.delete("value") { value_before_type_cast(object) }, options)
end
end
end
@@ -5,8 +5,8 @@ class TextField < Base # :nodoc:
def render
options = @options.stringify_keys
options["size"] = options["maxlength"] unless options.key?("size")
- options["type"] ||= field_type
- options["value"] = options.fetch("value"){ value_before_type_cast(object) } unless field_type == "file"
+ options["type"] ||= field_type
+ options["value"] = options.fetch("value") { value_before_type_cast(object) } unless field_type == "file"
options["value"] &&= ERB::Util.html_escape(options["value"])
add_default_name_and_id(options)
tag("input", options)
@@ -676,6 +676,13 @@ def test_text_area_with_alternate_value
)
end
+ def test_text_area_with_nil_alternate_value
+ assert_dom_equal(
+ %{<textarea id="post_body" name="post[body]">\n</textarea>},
+ text_area("post", "body", value: nil)
+ )
+ end
+
def test_text_area_with_html_entities
@post.body = "The HTML Entity for & is &amp;"
assert_dom_equal(

0 comments on commit 1424873

Please sign in to comment.