1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add link_to :back which uses your referrer with a fallback to a javascript link. #7366 [eventualbuddha, tarmo]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7791 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Michael Koziarski 2007-10-08 03:30:29 +00:00
parent 85c86f0999
commit 074fe35b8a
3 changed files with 24 additions and 4 deletions

View file

@ -1,5 +1,7 @@
*SVN* *SVN*
* Add link_to :back which uses your referrer with a fallback to a javascript link. #7366 [eventualbuddha, tarmo]
* error_messages_for and friends also work with local variables. #9699 [Frederick Cheung] * error_messages_for and friends also work with local variables. #9699 [Frederick Cheung]
* Fix url_for, redirect_to, etc. with :controller => :symbol instead of 'string'. #8562, #9525 [Justin Lynn, Tarmo Tänav, shoe] * Fix url_for, redirect_to, etc. with :controller => :symbol instead of 'string'. #8562, #9525 [Justin Lynn, Tarmo Tänav, shoe]

View file

@ -86,8 +86,9 @@ module ActionView
# of +options+. See the valid options in the documentation for # of +options+. See the valid options in the documentation for
# url_for. It's also possible to pass a string instead # url_for. It's also possible to pass a string instead
# of an options hash to get a link tag that uses the value of the string as the # of an options hash to get a link tag that uses the value of the string as the
# href for the link. If nil is passed as a name, the link itself will become # href for the link, or use +:back+ to link to the referrer - a JavaScript back
# the name. # link will be used in place of a referrer if none exists. If nil is passed as
# a name, the link itself will become the name.
# #
# ==== Options # ==== Options
# * <tt>:confirm => 'question?'</tt> -- This will add a JavaScript confirm # * <tt>:confirm => 'question?'</tt> -- This will add a JavaScript confirm
@ -134,7 +135,14 @@ module ActionView
# var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); # var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');
# m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a> # m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a>
def link_to(name, options = {}, html_options = nil) def link_to(name, options = {}, html_options = nil)
url = options.is_a?(String) ? options : self.url_for(options) url = case options
when String
options
when :back
@controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
else
self.url_for(options)
end
if html_options if html_options
html_options = html_options.stringify_keys html_options = html_options.stringify_keys

View file

@ -1,6 +1,6 @@
require "#{File.dirname(__FILE__)}/../abstract_unit" require "#{File.dirname(__FILE__)}/../abstract_unit"
RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port) RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port, :env)
class UrlHelperTest < Test::Unit::TestCase class UrlHelperTest < Test::Unit::TestCase
include ActionView::Helpers::AssetTagHelper include ActionView::Helpers::AssetTagHelper
@ -109,6 +109,16 @@ class UrlHelperTest < Test::Unit::TestCase
assert_dom_equal "<a href=\"http://www.example.com?q1=v1&amp;q2=v2\">http://www.example.com?q1=v1&amp;q2=v2</a>", link_to(nil, "http://www.example.com?q1=v1&amp;q2=v2") assert_dom_equal "<a href=\"http://www.example.com?q1=v1&amp;q2=v2\">http://www.example.com?q1=v1&amp;q2=v2</a>", link_to(nil, "http://www.example.com?q1=v1&amp;q2=v2")
end end
def test_link_tag_with_back
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {'HTTP_REFERER' => 'http://www.example.com/referer'})
assert_dom_equal "<a href=\"http://www.example.com/referer\">go back</a>", link_to('go back', :back)
end
def test_link_tag_with_back_and_no_referer
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {})
assert_dom_equal "<a href=\"javascript:history.back()\">go back</a>", link_to('go back', :back)
end
def test_link_tag_with_img def test_link_tag_with_img
assert_dom_equal "<a href=\"http://www.example.com\"><img src='/favicon.jpg' /></a>", link_to("<img src='/favicon.jpg' />", "http://www.example.com") assert_dom_equal "<a href=\"http://www.example.com\"><img src='/favicon.jpg' /></a>", link_to("<img src='/favicon.jpg' />", "http://www.example.com")
end end