Flowdock
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
Show source
Register or log in to add new notes.
July 4, 2008
6 thanks

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

Example

link_to name, url, :popup => ['dialog name','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes']
April 25, 2012 - (>= v3.1.0)
6 thanks

HTML5 data- attributes using RESTful approach

HTML5 specifies extensible attributes like data-foo=“bar” (or as in Twitter Bootstrap data-toggle=“modal”), which poses two problems for Rails.

First, if you’re using symbol notation in link_to to specify attributes, this fails (dash is not a valid symbol character), so

Invalid!

link_to "Edit", @user, :class => "btn", :data-toggle => "modal"

There are two solutions:

  1. put the symbols in quotes,

  2. use the special :data hash

Solution 1: Quote Symbols

link_to "Edit", @user, :class => "btn", "data-toggle" => "modal"

Solution 2: Use the :data hash

link_to "Edit", @user, :class => "btn", :data => {:toggle => "modal"}

Resulting HTML

<a href="/users/1" class="btn", data-toggle="modal">Edit</a>

The second is minimally documented, but as a hash, can accept multiple values and is perhaps a little cleaner

July 26, 2010 - (<= v2.3.8)
2 thanks

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

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
# => <a href="#" onclick="if(confirm('Are you sure?')) {destroyJsFunction()}; return false;">Delete</a>
# actual output
# => <a href="#" onclick="return confirm('Are you sure?');">Delete</a>

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.

February 17, 2009
2 thanks

Remember to sanitize name

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.

March 19, 2009
2 thanks

Expression

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'
March 7, 2011 - (>= v3.0.0)
1 thank

:method => :delete, etc.

If you’re upgrading to Rails 3 you’ll need to make sure you include rails.js (which is in public/javascripts when you rails new someproject) You’ll need to include it after prototype. And you’ll need to have a 1.7 version of prototype.

When you do a

link_to "Delete", @some_obj, :method => "delete", :confirm => "Are you sure?"

Rails 3 will generate

<a href="some_obj/id" data-confirm="Are you sure?" data-method="delete">Delete</a>

rails.js will creates observers that convert that into a form.

Be aware that this probably won’t work as a link from inside a form (since forms in forms isn’t valid).

November 10, 2011
0 thanks

logic in class/id

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’) %>

September 7, 2009
0 thanks

:popup gotcha in IE

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.