DEV Community

Jude
Jude

Posted on • Edited on

1

Ruby On Rails Beginners - Demystify the magic. Part 2: It's just a ruby method

Part 1 | Part 3

Following on from part 1, we are going to break down the link_to method. It's something you'll use almost as soon as you start working on a rails app, and it's fairly easy to understand.

The link_to method is referred to as a helper. A helper is just a ruby method that is available to use in Rails view files. Rails comes with built-in helpers, like link_to, but you can also create your own.

<%= link_to "Apple", "http://apple.com" %>
Enter fullscreen mode Exit fullscreen mode

or if you prefer to use parentheses

<%= link_to("Apple", "http://apple.com") %>
Enter fullscreen mode Exit fullscreen mode

Which will render out the following HTML code.

<a href="https://apple.com">Apple</a>
Enter fullscreen mode Exit fullscreen mode

To get an idea of what's going on here, we can recreate a basic version of the method ourselves.

def link_to(text, url)
   "<a href='#{url}'>#{text}</a>".html_safe
end
Enter fullscreen mode Exit fullscreen mode

And that it's. The built-in helper is more complex than this, but at it's most basic version this is all it does.

The built in helper can also take in a third argument, which is a hash where you can pass in HTML options, which you can learn more about here

In the next part we'll dive in to the rails api docs, and learn a little more about the link_to method.

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)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay