DEV Community

Jude
Jude

Posted on • Edited on

Ruby On Rails Beginners - Demystify the magic. Part 1: The Ruby looks a little different

This is a series where we try and look past the Ruby 'magic' and understand how to demystify Rails, starting from 0 to reading Rails source code! Scary stuff.

Part 2 | Part 3

When you first start learning Rails, it's usually after learning some basic programming in Ruby. This is still the best way to get into rails for a beginner, but making the jump can be a little bit confusing.

For me, the Ruby in Ruby on Rails looked different to what I was used to when I was writing Ruby, and when I was starting out I found it hard to understand what was going on. One of the reasons is that my eyes were used to seeing method arguments wrapped in parentheses method_name(arg1, arg2). Something which you commonly don't use in rails.

So to hopefully help demystify Rails for someone (anyone?) going through the same confusion, I'm going to use the link_to method, as it's one of the first things you will work within a Rails app.

What is link_to

Link_to is a method used in Ruby on Rails to create HTML links that point to a specific web address (url). It’s a powerful and flexible tool and it's an essential part of any Rails application.

It takes two arguments (it can also take a third argument and a block which we will cover later), the text to be displayed, and the target URL.

This is how you would usually see it, in a view file

<%= link_to β€œHome”, root_path %>
Enter fullscreen mode Exit fullscreen mode

This is what it looks like with parentheses

link_to("Home", root_path)
Enter fullscreen mode Exit fullscreen mode

For me on my first RoR day/week, this would have made a lot more sense. I think I missed the explanation that when you call a method in ruby that requires arguments, even if you don't use parentheses, ruby is smart enough to figure out that they must be arguments, so they are treated as such.

Because I missed that, I just put it down to that Rails magic that everyone keeps talking about.

This may seem really silly, and maybe it is, but if this helps anyone, even just a little bit, then I'm more than happy to look a little bit silly.

In part 2, I'll try and drive home that Rails is just plain old ruby, and we'll look at re-creating a basic version of the link_to method.

part 2 >>

Top comments (0)