Ah, yes.

The link_to method.

One of the most common helper methods you’ll use in your Rails applications.

But how does it work?

If you came here to learn about link_to & the different options you can use with it then you’re in the right place!

First…

What does link_to do?

Well, this whole website thing only works because we have links between pages.

That’s how you go from page to page.

In plain HTML, you create a link like this:

Improve Your Ruby Skills

But in Rails that would look like this:

<%= link_to="" "improve="" your="" ruby="" skills",="" "="" ruby-book"="" %="">

Why?

Because we use routes in Rails.

We want to take advantage of that by using _path methods & making that the target (href) of our link.

Using link_to makes this easier because we don’t have to interpolate the value.

This is what I mean:

Improve Your Ruby Skills

Now:

Let’s look deeper into the most helpful optional arguments supported by this method.

link_to Options & Arguments

The first argument for link_to is the text on the link.

The second argument?

It’s the URL you’re linking to.

You can hardcode it if you want, but most of the time you’ll be using a Rails model, or a _path method.

Rails will figure things out when you follow the proper conventions.

For example:

<%= link_to="" "improve="" your="" ruby="" skills",="" book_path(@book)="" %="">

Or:

<%= link_to="" "improve="" your="" ruby="" skills",="" @book="" %="">

How do you know which one to use?

  • Singular form when referring to a specific resource (book)
  • Plural form when referring to a collection (books)

Examples:

# Plural
<%= link_to="" "all="" books",="" books_path="" %="">

# Singular
<%= link_to="" "edit="" book",="" edit_book_path(@book)="" %="">

It also helps to look at your routes (with rake routes).

The 1st column has the name (like edit_book) that you can use in your links by adding _path to it.

Delete Link With Confirmation

Two helpful options you can use:

  • confirm
  • disable_with

Example:

<%= link_to="" "delete="" book",="" @book,="" method:="" "delete",="" {="" confirm:="" "are="" you="" sure?",="" disable_with:="" "processing..."="" }="" %="">

The default action for a link is a GET request.

If you want to use the “DELETE” action you need to be specific about it.

But…

What if you want a link to the previous page?

Try this:

<%= link_to="" "back",="" :back="" %="">

How to Use Custom CSS Classes & HTML Attributes

You can make your links look different using CSS.

For example:

<%= link_to="" "get="" more="" books",="" books_path,="" class:="" "index-link"="" %="">

Assuming you have this CSS:

.index-link {
  color: blue;
  padding: 10px;
}

If you need more HTML attributes (like “id”) you can add them after the link text & link URL.

How to Create Query Params & Anchor Links

Another option you may find helpful is the ability to create links with query parameters & anchors.

Let’s look at some examples!

If you want to create an URL like this:

"/search?q=all"

Then you can do this:

<%= link_to="" "search="" all",="" search_path(q:="" "all")="" %="">

And for this URL:

"/books#programming"

You can do this:

<%= link_to="" "programming="" books",="" books_path(anchor:="" "programming")="" %="">

Combined with URL helpers (_path / _url), link_to gives you all the flexibility you need.

Linking to Images

Many people don’t know that link_to takes an optional block.

This enables more complex scenarios.

Like linking to an image:

<%= link_to="" books_path="" do="" %="">
  <%= image_tag="" "book="" collection"="" %="">
<% end="" %="">

In this example, the content of the block becomes the linking text, image or any other HTML element that you want to become clickable.

Summary

You’ve learned about the link_to method in Rails!

If you found this article helpful get a copy of my Ruby book so you can super-charge your Ruby skills & support my work.

Thanks for reading 🙂