JT Book a call

Blog

Mastering Link Creation in Rails: Best Practices Unveiled

Published

3 min read

Mastering Link Creation in Rails: Best Practices Unveiled

In the world of Ruby on Rails, creating URLs efficiently is crucial for developers. This article covers the ways to build a link_to URL in a Rails app and when to pick each one. Every output below was run on Rails 8.1.4 and checked against the link_to API documentation.

Key Takeaways #

  • link_to is the Rails view helper for building anchor tags.
  • Path helpers such as article_path(article) are generated from your routes, so they follow the routes when those change.
  • HTML attributes go in a separate options hash; URL parameters go on the path helper.

The link_to method in Rails builds an anchor element. Its first argument is the text to display and its second is the URL, or something Rails can turn into one. A third hash holds HTML attributes. Here’s a basic example:

link_to "Home", root_path
# => <a href="/">Home</a>

Four Ways To Pass The URL #

Given routes with root "home#index" and resources :articles, only: %i[index show], these all render on Rails 8.1.4:

link_to "Article", article_path(7)
# => <a href="/articles/7">Article</a>

link_to "Article", {controller: "articles", action: "show", id: 7}
# => <a href="/articles/7">Article</a>

link_to "Search", articles_path(q: "ruby", page: 2)
# => <a href="/articles?page=2&amp;q=ruby">Search</a>

link_to "Hard-coded", "/articles/7"
# => <a href="/articles/7">Hard-coded</a>

The Rails guide on Action View helpers also shows that passing a model, link_to "Profile", @profile, works because Rails derives the URL from the record.

  1. Use Path Helpers: Instead of hardcoding URLs, use the helpers generated by Rails routing. The hard-coded string above renders the same link today, but if you later change that route’s path, the helper regenerates the new path and the string keeps pointing at the old one.
  2. Put HTML Attributes In The Last Hash: link_to "Article", article_path(7), class: "btn", id: "a", data: {turbo: false} renders <a class="btn" id="a" data-turbo="false" href="/articles/7">Article</a>. Query parameters belong on the path helper, as in articles_path(q: "ruby").
  3. Consider Accessibility: Use descriptive link text so a link makes sense on its own; WCAG 2.2 covers this under link purpose. Anchors with an href are keyboard focusable by default.

Common Pitfalls To Avoid #

  • Hardcoding URLs: A changed route path leaves the string pointing at the old URL. Prefer path helpers.
  • Overusing Inline Styles: Instead of inline styles, use CSS classes for better maintainability.
  • Vague Link Text: “Click here” tells a screen-reader user nothing when links are read out as a list.

Conclusion #

Use path helpers for internal links, pass HTML attributes in the last hash, and write link text that stands on its own. Those three habits keep links working through route changes and readable for everyone.

Read next

Tell us what you need built

JetThoughts has been building software since 2008. Book a call, or write to sales@jetthoughts.com. You'll leave the call with a written one-page scope within 2 working days.

#01 Paul Keen
Paul KeenFounder & Open Source Contributor

A 30-minute call; pick a time.