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

ChatGPT Image Mar 19, 2026, 11_39_11 PM

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.