DEV Community

Rails Designer
Rails Designer

Posted on • Originally published at railsdesigner.com

2

Rails link_to_if and link_to_unless

This article was originally published on Rails Designer


Rails is packed with nifty, small helpers that help you not reinvent the wheel. Every once in a while you remember one that you are sure some of you are not known with.

Today and I want to quickly go over: link_to_if and link_to_unless.

They both act the same as the, probably, known link_to helper, but, as you guessed, allow a conditional.

Basics

The syntax for link_to_if looks like this:

link_to_if(condition, name, options = {}, html_options = {}, &block)
Enter fullscreen mode Exit fullscreen mode

Or with actual values:

<%= link_to_if(Current.user.present?, "Profile", profile_path) %>
Enter fullscreen mode Exit fullscreen mode

This will, if Current.user.present? returns true, render a typical link_to, but if it returns false it will return just the text Profile.

link_to_unless accepts the same attributes, but does the opposite. Take this example:

<%= link_to_unless(current_page?(home_path), "Home", home_path) %>
Enter fullscreen mode Exit fullscreen mode

This will render a typical link_to unless current_page?(home_path) returns true. You can imagine this is useful in navigations.

Tweak the conditional render

You can also tweak what (if anything) is rendered when the conditional is (not) met. Like so:

<%= link_to_if(Current.user.has_role?(:admin), "Manage Users", admin_users_path) do %>
  <div>
    <p>You need to be an admin to manage users.</p>

    <p>If you believe this is an error, please contact support.</p>
  </div>
<% end %>
Enter fullscreen mode Exit fullscreen mode

I have used this to show links to features a certain account/user didn't have access to, but then show a modal for an upsell. Works great.

<%= link_to_if(Feature.enabled?(:ai_access), "Add AI Magic", setting_ai_path) do %>
  <%= link_to "Add AI Magic", upsell_path(highlight: "ai"), data: {turbo_frame: "modal"} %>
<% end %>
Enter fullscreen mode Exit fullscreen mode

And that's it. Yet another small, but useful little helper in Rails.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay