Even after many years of using Ruby on Rails I still have trouble remembering how to properly use all those options available for the link_to helper. Three out of five times I have to fire up the docs and search for the examples (cause that’s how I like to read the docs).
Having Documentation in Hand
Using documentation locally is helpful, especially if you can integrate it into your editor. Personally I’m using the
ri docs with Vim but I guess all those other editors can be configured to access it. If not you can always use ri in your terminal of choice.
Another favourite of mine is the awesome ApiDock. You can even see other people’s comments on the docs and in many cases there you can find examples that are not present in the official docs.
Another very good option (if you’re on a mac) is to use Dash. It’s easy to integrate in your editor or you can just fire it up with a system shortcut whenever you need to access the docs.
That being said, I’m gonna list out a few of the more common examples I use in hopes that they will be useful for both you and me whenever I need to revisit
link_to’s docs again.
The Simplest Form
The most common and straightforward way of using
link_to is to create a barebones (no magic) link. So, assuming your root route points to ‘/’, the way you would use the helper is:
Link-ing to a Resource
Another common variant is linking to a resource (a user’s profile for example). Let’s say the user’s model is called User and that a @user ivar points to the user record who’s id is 1. Creating a link to the user’s profile will look like the following:
There’s also a shorter version that will do the same thing. It’s got some magic to it (like most things in Rails) but it also looks prettier and there’s less to type (two stones with one bird).
Using link_to with a Block
This is somewhat of a lesser known/used option of
link_to but it’s useful nonetheless and it also makes the code more readable. So in those cases where the link text is long and/or ugly, or it doesn’t really fit on a 80 chars line, you can pass the link text inside a block.
To make the example more obvious, I’m gonna do a before and after kind of thing.
1 2 3 4
In this case I’ve purposely chosen a less uglier link text, but usually the link text will be something like an image tag or a span with an icon inside it (or any other ugly html code you can think of).
Adding html classes and/or id to Your Link
Another very common task you’ll use is to add a html class or id to your links.
How to Delete a Record with link_to
destroy action of a REST-ful controller requires a
DELETE request and that can be easily achieved by passing the
:method => :delete hash as an option to the
Note that the
rel="nofollow" is auto-magically added by Rails as an SEO bonus.
Require Confirmation for Deleting a Record
Link-ing to an Image with link_to
It might be that you want to make your links prettier or that you want to have some nice buttons, or even a logo click-able or whatever the reason for using click-able images is, you’ll want to add your image inside the link. Making an image link-able is pretty straight forward. Just add the
image_tag where the link text would go and you’re done.
You can also pass the image in a block if you like that style better.
1 2 3 4
A nice side-effect of using the
image_tag helper is that it will add the asset digest to your image.
Adding an alt Attribute to the Image
As you’ve seen in the previous example, I didn’t specify an alt attribute but the link_to helper generated one. The generated alt tag is just the name of the image file, capitalized. In case you want (and you should want) to override the alt attribute, it’s very easy to do; just add your own alt attribute like so:
Alt attributes are beneficial for SEO purposes and they also aid those visitors who use text readers or non graphical browsers.
Link-ing to an Image
There are times when you might want to link to an image (not necessarily with an image). This can be confusing because you need your image to contain the image digest generated by the asset pipeline. There’s a helper that provides just that and it’s called
Anchors with link_to
You might need to point to a specific section (anchor) in the page which you can identify by it’s dom ID. So let’s say on the target page we have a section that has the
id="interesting-section". In order to point our link to that section, we’ll need to add the anchor to the generated link.
Ajax Links with link_to Remote
You can add the
Opening the link in a new tab or window
For a good user experience and because you’ll want your user not to leave your website if possible, you should make all your external links open in a separate tab or window. You can achieve this by using the
target="_blank" html attribute which in Rails speak will look like this:
POST-ing Using link_to
Sending a post request via a link is something that the html cannot do. You can only use it to make
GET requests, not
POST. That being said, Rails has some magic tricks for you.
By providing the
jquery-rails gem for this to work, if you don’t have it, there won’t be any magic happening and your links will default to a GET request.
Adding More Params to the POST Request
These are some of the more common ways I’ve used link_to but I’m sure there are many others. So if you have any other examples I could add to the article.
So far so good, That’s it!!! See ya!!! :)