How to Use Rails link_to Method (With Examples)

Rails link_to method!

One of the most common helper methods you’ll use in all 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.

Rails 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.

Why is that helpful?

Because you’ll be able to access this additional data from your controller after the user clicks the link.

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 🙂

10 thoughts on “How to Use Rails link_to Method (With Examples)”

  1. You forgot including format and array style like link_to [@book, format: :pdf].
    Thanks for your work

Comments are closed.