Mastering Ruby on Rails Views
π¨ Mastering Ruby on Rails Views: Hidden Tricks, Performance Hacks & Gems You MUST Know π
When building a Ruby on Rails application, Views are where your users actually experience your product. But most developers only scratch the surface β using basic ERB templates without unlocking the true power of Rails Views.
In this blog, weβll uncover: β Hidden features β Smart tricks β Performance optimizations β Powerful gems β Real-world examples
Letβs transform your views from basic templates β high-performance UI engines π‘
π§ What Are Rails Views (Quick Recap)
Rails Views are responsible for rendering the UI using:
- ERB (
.html.erb) - HAML / SLIM
- Partials
- Layouts
They follow MVC: π Controller β prepares data π View β displays data
π₯ 1. Hidden Features of Rails Views You Probably Missed
π§© 1.1 content_for & yield Magic
Used for dynamic layouts.
<% content_for :title do %>
Dashboard
<% end %>
In layout:
<title><%= yield :title %></title>
π‘ Use Case: Dynamic page titles, scripts, meta tags
π§© 1.2 capture for Reusable Blocks
<% greeting = capture do %>
Hello, <%= current_user.name %>
<% end %>
<%= greeting %>
π‘ Helps build reusable UI snippets dynamically.
π§© 1.3 provide (Cleaner Alternative)
<% provide(:title, "Home") %>
β Cleaner than content_for
π§© 1.4 render Variations (Super Powerful)
<%= render @users %>
Rails automatically picks _user.html.erb
π‘ This is called Collection Rendering β super efficient!
β‘ 2. Performance Optimization Techniques
π 2.1 Fragment Caching (Must Know)
<% cache @product do %>
<%= render @product %>
<% end %>
π‘ Rails stores this block β avoids re-rendering
π 2.2 Russian Doll Caching πͺ
<% cache @post do %>
<%= render @post.comments %>
<% end %>
π‘ Nested caching for complex UI
π 2.3 Avoid N+1 Queries in Views
β Bad:
<% @posts.each do |post| %>
<%= post.user.name %>
<% end %>
β Fix in controller:
@posts = Post.includes(:user)
π 2.4 Use pluck Instead of Full Objects
User.pluck(:name)
π‘ Faster than loading entire records
π 2.5 Streaming Views (Advanced β‘)
render stream: true
π‘ Sends response in chunks β faster perceived load
π§ 3. Pro-Level View Tricks
π― 3.1 View Helpers for Clean Code
def format_price(price)
number_to_currency(price)
end
Use in view:
<%= format_price(100) %>
β Keeps views clean and readable
π― 3.2 Safe HTML Rendering
<%= sanitize(user_input) %>
β οΈ Avoid:
<%= raw user_input %>
π‘ Prevents XSS attacks
π― 3.3 Conditional Rendering Shortcut
<%= render @post if @post.present? %>
π― 3.4 Dynamic Partial Names
<%= render "cards/#{@user.role}" %>
π‘ Super useful for role-based UI
π 4. Powerful Gems for Rails Views
π 4.1 Draper (Decorator Pattern)
π Clean separation of logic
@user.decorate.full_name
β Keeps views logic-free
π 4.2 ViewComponent (by GitHub)
Component-based views:
render(UserComponent.new(user: @user))
π‘ Scalable UI architecture
π 4.3 Slim / HAML (Cleaner Syntax)
Slim example:
h1 Hello #{@user.name}
β Less code, cleaner UI
π 4.4 Cells (View Models)
cell(:comment, @comment)
π‘ Encapsulates view logic
π 4.5 Phlex (Modern Approach π)
Ruby-based views:
class Card < Phlex::HTML
def template
h1 "Hello World"
end
end
π§± 5. Folder Structure Best Practices
app/views/
users/
index.html.erb
_user.html.erb
shared/
_navbar.html.erb
_footer.html.erb
β Use shared/ for reusable components
β Keep partials small & focused
π§ͺ 6. Real-World Example (Optimized View)
<% cache @users do %>
<%= render @users %>
<% end %>
Controller:
@users = User.includes(:profile)
Partial:
<div class="user-card">
<h3><%= user.name %></h3>
</div>
π₯ Result:
- Faster rendering
- Clean code
- Scalable UI
β οΈ 7. Common Mistakes to Avoid
β Heavy logic in views
β Too many partials (over-fragmentation)
β Ignoring caching
β Using raw dangerously
β Not optimizing DB queries
π― Final Thoughts
Rails Views are not just templates β they are a performance layer + UX engine.
π Master these:
- Caching strategies
- Partial rendering
- Helper usage
- Component-based design
And your app will: π Load faster π¨ Look cleaner π Scale better
π¬ Powerful Quote
βFirst impressions are everything β and in web apps, your Views ARE the first impression.β π‘
π₯ Bonus Tip
If you want to level up even more: π Combine Rails Views + React (Hybrid Approach) π Use Hotwire (Turbo + Stimulus) for modern UI without heavy JS
© Lakhveer Singh Rajput - Blogs. All Rights Reserved.