public Method

UrlHelper.link_to(name, options = {}, html_options = nil)

Contents:

Creates a link tag of the given name using a URL created by the set of options. See the valid options in the documentation for 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 href for the link, or use +:back+ to link to the referrer - a JavaScript back 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

  • :confirm => ‘question?’ — This will add a JavaScript confirm prompt with the question specified. If the user accepts, the link is processed normally, otherwise no action is taken.
  • :popup => true || array of window options — This will force the link to open in a popup window. By passing true, a default browser window will be opened with the URL. You can also specify an array of options that are passed-thru to JavaScripts window.open method.
  • :method => symbol of HTTP verb — This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site). Supported verbs are :post, :delete and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET. If you are relying on the POST behavior, you should check for it in your controller’s action by using the request object’s methods for post?, delete? or put?.
  • The html_options will accept a hash of html attributes for the link tag.

Note that if the user has JavaScript disabled, the request will fall back to using GET. If :href=>’#’ is used and the user has JavaScript disabled clicking the link will have no effect. If you are relying on the POST behavior, your should check for it in your controller’s action by using the request object’s methods for post?, delete? or put?.

You can mix and match the html_options with the exception of :popup and :method which will raise an ActionView::ActionViewError exception.

Examples

link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?"
# => <a href="http://www.rubyonrails.org/" onclick="return confirm('Are you sure?');">Visit Other Site</a>

link_to "Help", { :action => "help" }, :popup => true
# => <a href="/testing/help/" onclick="window.open(this.href);return false;">Help</a>

link_to "View Image", { :action => "view" }, :popup => ['new_window_name', 'height=300,width=600']
# => <a href="/testing/view/" onclick="window.open(this.href,'new_window_name','height=300,width=600');return false;">View Image</a>

link_to "Delete Image", { :action => "delete", :id => @image.id }, :confirm => "Are you sure?", :method => :delete
# => <a href="/testing/delete/9/" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form');
     f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;
     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>

Source Code

# File action_view/helpers/url_helper.rb, line 137
def link_to(name, options = {}, html_options = nil)
  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
    html_options = html_options.stringify_keys
    href = html_options['href']
    convert_options_to_javascript!(html_options, url)
    tag_options = tag_options(html_options)
  else
    tag_options = nil
  end

  href_attr = "href=\"#{url}\"" unless href
  "<a #{href_attr}#{tag_options}>#{name || url}</a>"
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.