method

link_to

link_to(name, options = {}, html_options = nil, *parameters_for_method_reference)
public

Creates a link tag of the given name using an URL created by the set of options. See the valid options in ActionView::Helpers::UrlHelper. It’s also possible to pass a string instead of an options hash to get a link tag that just points without consideration. If nil is passed as a name, the link itself will become the name.

The html_options has three special features. One for creating javascript confirm alerts where if you pass :confirm => ‘Are you sure?’, the link will be guarded with a JS popup asking that question. If the user accepts, the link is processed, otherwise not.

Another for creating a popup window, which is done by either passing :popup with true or the options of the window in Javascript form.

And a third for making the link do a POST request (instead of the regular GET) through a dynamically added form element that is instantly submitted. Note that if the user has turned off Javascript, the request will fall back on the GET. So its your responsibility to determine what the action should be once it arrives at the controller. The POST form is turned on by passing :post as true. Note, it’s not possible to use POST requests and popup targets at the same time (an exception will be thrown).

Examples:

  link_to "Delete this page", { :action => "destroy", :id => @page.id }, :confirm => "Are you sure?"
  link_to "Help", { :action => "help" }, :popup => true
  link_to "Busy loop", { :action => "busy" }, :popup => ['new_window', 'height=300,width=600']
  link_to "Destroy account", { :action => "destroy" }, :confirm => "Are you sure?", :post => true

13Notes

Link to caller URL

dmantilla · Oct 1, 200815 thanks
link_to "Back", :back

Link to same URL with different format

hannu · Jul 4, 200813 thanks

Use +params+.+merge+ as options. Ex.

<%= link_to "RSS feed", params.merge(:format => :rss), :class => "feed_link" %>

Opening a link in a new window

zoopzoop · Oct 20, 20088 thanks

Use "_blank", not "_new" to open a link in a new window. link_to "External link", "http://foo.bar", :target => "_blank" # => External link

link_to some url with current params

bansalakhil · Jan 9, 20098 thanks

==== Code example

link_to "some text", users_path(:params => params, :more_params => "more params")

Window open a dialog of no menu, no status, have scroll

RobinWu · Jul 4, 20086 thanks

==== Example

link_to name, url, :popup => ['dialog name','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes']

Anchor tag in link_to

bansalakhil · Feb 18, 20096 thanks

==== Code example

link_to("some text", articles_path(:anchor => "comment"))

will output some text

Remember to sanitize name

ville · Feb 17, 20092 thanks

While useful when in need of richer markup inside a link, the name parameter isn't sanitized or escaped and thus should be escaped when its content can't be guaranteed to be safe.

E.g. link_to(url, url)

may cause problems with character entities if url contains ampersands.

===== Correct usage

link_to(h(url), url)

This applies to all dynamic content.

Expression

r13 · Mar 19, 20092 thanks

You can put some expression too. For example for I18n (using haml on view):

some_locale.yml

links:
contacts: "My contacts"

index.html.haml

= link_to "#{t "links.contacts"}", :action => 'contacts'

:confirm, :popup, and :method override :onclick

metavida · Jul 26, 20102 thanks

upplying any combination of +:confirm+, +:popup+, and/or +:method+ options to the link_to method results the +:onclick+ option being overridden.

Example: link_to "Delete", '#', :confirm=>"Are you sure?", :onclick=>"destroyJsFunction()" # expected output # => Delete # actual output # => Delete

Note that the actual output doesn't include any mention of the "destroyJsFunction()" passed to the link_to method.

Rails 3 will use unobtrusive JavaScript, and I haven't tested how that will interact with the +:onclick+ option.

Text and Image together in #link_to

ShrutiGupta · Sep 3, 20151 thank

Code Example

link_to "Hello World #{ image_tag('web/worl.png') }".html_safe, some_path

:popup gotcha in IE

bquorning · Sep 7, 2009

If your popup title contains spaces or escaped HTML characters, Internet Explorer (at least 6/7) will not pop up a new window but open the link in the existing browser window.

logic in class/id

Graffzon · Nov 10, 2011

If you need to place some simple logic in class or like that, I think that best to make it with simple brackets:

====Code example

<%= link_to 'All actions', switch_action_tab_path, :remote => true, :class => ('selected' if @tab == 'all') %>

link_to with :as routing

danwich · Jan 16, 2012

The following will not work when your post model is routed with the :as option: link_to("View post", @post) Instead, use the helper with your custom name: link_to("View post", :url => renamedpost_path(@post))