ERB Şablonları: En İyi Uygulamalar ve İpuçları

ERB (Embedded Ruby), Rails için varsayılan şablon motorudur. Temiz ve sürdürülebilir ERB şablonları yazma en iyi uygulamaları:

1. Mantığı View’lardan Uzak Tutun

Kötü Örnek

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<!-- Bunu yapmayın -->
<% if user.admin? && user.active? && user.posts.count > 0 %>
  <div class="admin-panel">
    <% user.posts.each do |post| %>
      <% if post.published? && post.created_at > 1.week.ago %>
        <div class="recent-post">
          <%= post.title %>
        </div>
      <% end %>
    <% end %>
  </div>
<% end %>

İyi Örnek

1
2
3
4
5
6
<!-- Helper'ları ve model method'larını kullanın -->
<% if show_admin_panel?(user) %>
  <div class="admin-panel">
    <%= render 'admin/recent_posts', posts: user.recent_published_posts %>
  </div>
<% end %>
1
2
3
4
5
6
7
8
9
# Helper'da
def show_admin_panel?(user)
  user.admin? && user.active? && user.posts.any?
end

# User model'inde
def recent_published_posts
  posts.published.where('created_at > ?', 1.week.ago)
end

2. Yeniden Kullanılabilir Bileşenler için Partial’ları Kullanın

Modüler Bileşenler Oluşturun

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<!-- _user_card.html.erb -->
<div class="user-card">
  <div class="user-avatar">
    <%= image_tag user.avatar.present? ? user.avatar : 'default-avatar.png' %>
  </div>
  <div class="user-info">
    <h3><%= user.name %></h3>
    <p><%= user.email %></p>
    <span class="user-role"><%= user.role.humanize %></span>
  </div>
</div>
1
2
3
4
5
6
<!-- Partial'ı kullanın -->
<div class="users-grid">
  <% @users.each do |user| %>
    <%= render 'user_card', user: user %>
  <% end %>
</div>

3. HTML Güvenliği ve Kaçırma

Doğru HTML Kaçırma

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<!-- Varsayılan olarak güvenli -->
<p>Kullanıcı yorumu: <%= @comment.content %></p>

<!-- Ham HTML gerektiğinde (dikkatli olun!) -->
<div class="content">
  <%= raw @post.html_content %>
</div>

<!-- Sanitizasyon ile daha iyi yaklaşım -->
<div class="content">
  <%= sanitize @post.content, tags: %w[p br strong em ul ol li] %>
</div>

Dinamik HTML için content_tag Kullanma

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Helper'da
def status_badge(status)
  css_class = case status
              when 'active' then 'badge-success'
              when 'pending' then 'badge-warning'
              else 'badge-default'
              end
  
  content_tag :span, status.humanize, class: "badge #{css_class}"
end
1
2
<!-- Şablonda -->
<%= status_badge(user.status) %>

4. Koşullu Rendering

Temiz Koşullu Mantık

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<!-- İyi -->
<%= content_tag :div, class: "user-status #{'active' if user.active?}" do %>
  <span><%= user.name %></span>
  <% if user.admin? %>
    <span class="admin-badge">Admin</span>
  <% end %>
<% end %>

<!-- Helper ile daha iyi -->
<%= render 'user_status', user: user %>

5. Form En İyi Uygulamaları

Anlamsal Form Yapısı

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<%= form_with model: @user, local: true do |form| %>
  <div class="form-group">
    <%= form.label :name %>
    <%= form.text_field :name, class: 'form-control' %>
    <%= form.error_span :name %>
  </div>
  
  <div class="form-group">
    <%= form.label :email %>
    <%= form.email_field :email, class: 'form-control' %>
    <%= form.error_span :email %>
  </div>
  
  <div class="form-actions">
    <%= form.submit 'Kullanıcıyı Kaydet', class: 'btn btn-primary' %>
    <%= link_to 'İptal', users_path, class: 'btn btn-secondary' %>
  </div>
<% end %>

Özel Form Helper’ı

1
2
3
4
5
6
7
8
9
# application_helper.rb'de
module ApplicationHelper
  def form_error_span(form, field)
    if form.object.errors[field].any?
      content_tag :span, form.object.errors[field].first, 
                  class: 'error-message'
    end
  end
end

Sonuç

Bu ERB en iyi uygulamalarını takip etmek şablonlarınızı daha sürdürülebilir, güvenli ve debug etmesi kolay hale getirir. View’ları basit tutmayı ve karmaşık mantığı helper’lara veya model’lere taşımayı unutmayın.