Partials have been an integral part of Rails. They are conceptually simple to understand, but they pack quite a few smart and lesser known features yo

Rails' Partial Features You (didn't) Know

submited by
Style Pass
2024-11-16 23:30:03

Partials have been an integral part of Rails. They are conceptually simple to understand, but they pack quite a few smart and lesser known features you might not know about. Let’s look at all of them!

This will render the partial app/views/application/_navigation.html.erb. The filename starts with an underscore, but is referenced without one.

Quite a bit cleaner, right? So when does one use the short-hand version and when not? Typically you can use the short-hand version, especially when you are only passing 1 or 2 variables. But you can also pass other arguments; then it’s required to use the longer syntax. Like this example:

This is assuming you have those resources, and each has their respective partial, e.g. _user.html.erb, _admin.html.erb and _customer.html.erb.

You can render a counter too: <%= user_counter %>. The name, the part before the underscore, is derived from the partial name. So _customer.html.erb would be <%= customer_counter %>.

Leave a Comment