-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added FormTagHelper that provides a number of methods for creating fo…
…rm tags that doesn't rely on conventions with an object assigned to the template like FormHelper does. With the FormTagHelper, you provide the names and values yourself. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@687 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
Showing
4 changed files
with
83 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'cgi' | ||
require File.dirname(__FILE__) + '/tag_helper' | ||
|
||
module ActionView | ||
module Helpers | ||
# Provides a number of methods for creating form tags that doesn't rely on conventions with an object assigned to the template like | ||
# FormHelper does. With the FormTagHelper, you provide the names and values yourself. | ||
module FormTagHelper | ||
# Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like | ||
# ActionController::Base#url_for. The method for the form defaults to POST. | ||
# | ||
# Options: | ||
# * <tt>:multipart</tt> - If set to true, the enctype is set to "multipart/form-data". | ||
def form_tag(url_for_options = {}, options = {}, *parameters_for_url) | ||
html_options = { "method" => "post" }.merge(options) | ||
|
||
if html_options[:multipart] | ||
html_options["enctype"] = "multipart/form-data" | ||
html_options.delete(:multipart) | ||
end | ||
|
||
html_options["action"] = url_for(url_for_options, *parameters_for_url) | ||
|
||
tag("form", html_options, true) | ||
end | ||
|
||
alias_method :start_form_tag, :form_tag | ||
|
||
# Outputs "</form>" | ||
def end_form_tag | ||
"</form>" | ||
end | ||
|
||
def text_field_tag(name, value = nil, options = {}) | ||
tag("input", {"type" => "text", "name" => name, "id" => name, "value" => value}.update(options)) | ||
end | ||
|
||
def password_field_tag(name = "password", value = nil, options = {}) | ||
tag("input", {"type" => "password", "name" => name, "id" => name, "value" => value}.update(options)) | ||
end | ||
|
||
def text_area_tag(name, content = nil, options = {}) | ||
if options[:size] | ||
options["cols"], options["rows"] = options[:size].split("x") | ||
options.delete(:size) | ||
end | ||
|
||
content_tag("textarea", content, { "name" => name, "id" => name }.update(options)) | ||
end | ||
|
||
def check_box_tag(name, value = "1", checked = false, options = {}) | ||
html_options = {"type" => "checkbox", "name" => name, "id" => name, "value" => value}.update(options) | ||
html_options["checked"] = "checked" if checked | ||
tag("input", html_options) | ||
end | ||
|
||
def submit_tag(value = "Save changes", options = {}) | ||
tag("input", {"type" => "submit", "name" => "submit", "value" => value}.update(options)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'test/unit' | ||
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_tag_helper' | ||
|
||
class TagHelperTest < Test::Unit::TestCase | ||
include ActionView::Helpers::TagHelper | ||
include ActionView::Helpers::FormTagHelper | ||
|
||
MethodToTag = { | ||
%(text_field_tag("title", "Hello!")) => %(<input id="title" name="title" type="text" value="Hello!" />), | ||
%(text_field_tag("title", "Hello!", "class" => "admin")) => %(<input class="admin" id="title" name="title" type="text" value="Hello!" />), | ||
%(password_field_tag) => %(<input id="password" name="password" type="password" value="" />), | ||
%(text_area_tag("body", "hello world", :size => "20x40")) => %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>), | ||
%(check_box_tag("admin")) => %(<input id="admin" name="admin" type="checkbox" value="1" />), | ||
} | ||
|
||
def test_tags | ||
MethodToTag.each { |method, tag| assert_equal(eval(method), tag) } | ||
end | ||
end |