Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7791

Show
Ignore:
Timestamp:
10/08/07 03:30:29 (2 years ago)
Author:
nzkoz
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r7779 r7791  
    11*SVN* 
     2 
     3* Add link_to :back which uses your referrer with a fallback to a javascript link. #7366 [eventualbuddha, tarmo] 
    24 
    35* error_messages_for and friends also work with local variables.  #9699 [Frederick Cheung] 
  • trunk/actionpack/lib/action_view/helpers/url_helper.rb

    r7668 r7791  
    8787      # url_for. It's also possible to pass a string instead 
    8888      # of an options hash to get a link tag that uses the value of the string as the 
    89       # href for the link. If nil is passed as a name, the link itself will become 
    90       # the name. 
     89      # href for the link, or use +:back+ to link to the referrer - a JavaScript back 
     90      # link will be used in place of a referrer if none exists. If nil is passed as 
     91      # a name, the link itself will become the name. 
    9192      # 
    9293      # ==== Options 
     
    135136      #        m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a> 
    136137      def link_to(name, options = {}, html_options = nil) 
    137         url = options.is_a?(String) ? options : self.url_for(options) 
     138        url = case options 
     139          when String 
     140            options 
     141          when :back 
     142            @controller.request.env["HTTP_REFERER"] || 'javascript:history.back()' 
     143          else 
     144            self.url_for(options) 
     145          end 
    138146 
    139147        if html_options 
  • trunk/actionpack/test/template/url_helper_test.rb

    r7668 r7791  
    11require "#{File.dirname(__FILE__)}/../abstract_unit" 
    22 
    3 RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port
     3RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port, :env
    44 
    55class UrlHelperTest < Test::Unit::TestCase 
     
    108108  def test_link_tag_with_query_and_no_name 
    109109    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") 
     110  end 
     111 
     112  def test_link_tag_with_back 
     113    @controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {'HTTP_REFERER' => 'http://www.example.com/referer'}) 
     114    assert_dom_equal "<a href=\"http://www.example.com/referer\">go back</a>", link_to('go back', :back) 
     115  end 
     116   
     117  def test_link_tag_with_back_and_no_referer 
     118    @controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {}) 
     119    assert_dom_equal "<a href=\"javascript:history.back()\">go back</a>", link_to('go back', :back) 
    110120  end 
    111121