[λ²μ] Ruby on Rails View Patterns and Anti-patterns(Ruby On Railsμ Viewμ ν¨ν΄κ³Ό μν°ν¨ν΄)
π‘ μλ³ΈκΈ : https://blog.appsignal.com/2021/02/10/ruby-on-rails-view-patterns-and-anti-patterns.html
Ruby on Rails View Patterns and Anti-patterns | AppSignal Blog
Rails views are sometimes amazing and fast, and at other times, they can have all sorts of issues. If you want to increase confidence over how you handle your views, then this blog post is for you.
blog.appsignal.com
Ruby On Rails ν¨ν΄κ³Ό μν°ν¨ν΄ μ리μ¦μ μΈ λ²μ§Έ νΈμ μ€μ κ±Έ νμν©λλ€. μ΄μ κ²μλ¬Όμμ μ°λ¦¬λ Rails λͺ¨λΈκ³Ό κ΄λ ¨ν ν¨ν΄κ³Ό μν°ν¨ν΄κ³Ό μΌλ°μ μΈ κ²½μ°μ λν΄μ λ€λ£¨μμ΅λλ€. μ΄λ² κΈμμλ Railμ viewμ κ΄λ ¨λ λͺ κ°μ§ ν¨ν΄κ³Ό μν°ν¨ν΄μ λν΄μ μ΄ν΄λ³΄κ² μ΅λλ€.
Rails Viewλ λλ‘λ μλ²½νκ³ λΉ λ₯΄κ² λμνκΈ°λ νμ§λ§, μ¨κ° μ’ λ₯μ λ¬Έμ κ° λ°μν μ μμ΅λλ€. Viewλ₯Ό μ²λ¦¬νλ λ°©λ²μ λν μμ κ°μ λμ΄κ³ μΆκ±°λ μ΄ μ£Όμ μ λν΄ λ μμΈν μκ³ μΆλ€λ©΄ μ΄ λΈλ‘κ·Έ κ²μλ¬Όμ΄ λμμ΄ λ κ²μ λλ€. κ·ΈλΌ λ°λ‘ μμνκ² μ΅λλ€.
μμλ€μνΌ Rails νλ μμν¬λ ꡬμ±μ λν κ·μΉμ λ°λ¦ λλ€. κ·Έλ¦¬κ³ Railsλ λͺ¨λΈ-λ·°-컨νΈλ‘€λ¬(MVC) ν¨ν΄μ μ€μμνκΈ° λλ¬Έμ μ΄ λͺ¨ν λ μμ°μ€λ½κ² View μ½λμλ μ μ©λ©λλ€. μ¬κΈ°μλ λ§ν¬μ (ERB λλ Slim νμΌ), JavaScript λ° CSS νμΌμ΄ ν¬ν¨λ©λλ€. μΈλ» 보기μλ View λ μ΄μ΄κ° λ§€μ° κ°λ¨νκ³ μ½λ€κ³ μκ°ν μ μμ§λ§ μμ¦μλ View λ μ΄μ΄μ μ¬λ¬κ°μ§ κΈ°μ μ΄ νΌμ¬λμ΄ μλ€λ μ μ λͺ μ¬νμΈμ.
Viewμλ JavaScript, HTML, CSSκ° μ¬μ©λ©λλ€. μ΄ μΈκ°μ§ κΈ°μ μ νΌλκ³Ό μ½λμ 무μ§μλ₯Ό μ΄λνμ¬ ν° μλ―Έκ° μλ ꡬνμΌλ‘ μ΄μ΄μ§μ μμ΅λλ€. λ€ννλ μ€λμ Rails View κ³μΈ΅μ λͺ κ°μ§ μΌλ°μ μΈ λ¬Έμ μ ν΄κ²° λ°©λ²μ μ΄ν΄λ³΄κ² μ΅λλ€.
Powerlifting Views
μ΄ μ€μλ μμ£Ό λ°μνμ§ μμ§λ§,, λ°μνλ©΄ λμ κ±°μ¬λ¦¬λ μ€μμ λλ€. λλλ‘ μ¬λλ€μ λλ©μΈ λ‘μ§μ΄λ 쿼리λ₯Ό View λ΄λΆμ μ§μ λ£λ κ²½ν₯μ΄ μμ΅λλ€. μ΄λ κ² νλ©΄ View λ μ΄μ΄κ° λ¬΄κ±°μ΄ μμ μ΄λ νμ리νν (Powerlifting)μ μννκ² λ©λλ€. ν₯λ―Έλ‘μ΄ μ μ Railsλ₯Ό μ¬μ©νλ©΄ μ€μ μ΄λ° μΌμ΄ μ½κ² μΌμ΄λ μ μλ€λ κ²μ λλ€. μ΄μ κ΄λ ¨νμ¬ βμμ λ§βμ΄ μκΈ° λλ¬Έμ View λ μ΄μ΄κ° μνλ 무μμ΄λ λ€ ν μκ° μμ΅λλ€.
MVCν¨ν΄μ View λ μ΄μ΄μ μ μμ λ°λΌ ν΄λΉ λ μ΄μ΄μλ ν리μ ν μ΄μ (νν)νλ λ‘μ§μ΄ ν¬ν¨λμ΄μΌ ν©λλ€. λλ©μΈ λ‘μ§μ΄λ λ°μ΄ν° 쿼리μλ μ κ²½ μ°μ§ μμμΌ ν©λλ€. Railsμλ λ£¨λΉ μ½λλ₯Ό μμ±ν μ μλ ERB νμΌ(μλ² λλ 루λΉ)κ° μ 곡λλ©°, μ΄ νμΌμ HTMLλ‘ νκ°λ©λλ€. index νμ΄μ§μ λ Έλλ₯Ό λμ΄νλ μΉμ¬μ΄νΈμ μλ₯Ό μκ°ν΄λ³΄λ©΄, View λ‘μ μ app/views/songs/index.html.erbμ μμ κ²μ λλ€.
νμ리νν (Powerlifting)μ΄ λ¬΄μμ μλ―Ένκ³ λ¬΄μμ νμ§ λ§μμΌ νλμ§μ λν΄ μ€λͺ νκΈ° μν΄ λ€μ μμ λ₯Ό μ΄ν΄λ³΄κ² μ΅λλ€:
# app/views/songs/index.html.erb
<div class="songs">
<% Song.where(published: true).order(:title) do |song| %>
<section id="song_<%= song.id %>">
<span><%= song.title %></span>
<span><%= song.description %></span>
<a href="<%= song.download_url %>">Download</a>
</section>
<% end %>
</div>
μ¬κΈ°μ ν¬κ² 보μ¬μ§λ μν°ν¨ν΄μ λ§ν¬μ μμ λ°λ‘ Songμ κ°μ Έμ€λ βSong.where(published: true).order(:title)β μ½λλΆλΆμ λλ€. λ°μ΄ν°λ₯Ό κ°μ Έμ€λ μ± μμ Controller λλ Serviceμ μμν΄μΌ νλλ° κ°λ Controllerμμ μΌλΆ λ°μ΄ν°λ₯Ό μ€λΉν νμ λμ€μ Viewμμ λ λ§μ λ°μ΄ν°λ₯Ό κ°μ Έμ€λ κ²½μ°λ₯Ό λ΄ λλ€. μ΄λ μλͺ»λ μ€κ³μ΄λ©° μΏΌλ¦¬λ‘ λ°μ΄ν°λ² μ΄μ€μ λ λΆνλ₯Ό μ£ΌκΈ° λλ¬Έμ μΉμ¬μ΄νΈ μλκ° λλ €μ§λλ€.
λ°λΌμ μμ κ°μ λ°©μ λμ μ Controller μ‘μ μμ β@Songβμ μΈμ€ν΄μ€ λ³μλ₯Ό λ ΈμΆνκ³ λ€μκ³Ό κ°μ΄ λ§ν¬μ μμ νΈμΆνλ κ²μ΄ μ’μ΅λλ€.
class SongsController < ApplicationController
...
def index
@songs = Song.all.where(published: true).order(:title)
end
...
end
# app/views/songs/index.html.erb
<div class="songs">
<% @songs.each do |song| %>
<section id="song_<%= song.id %>">
<span><%= song.title %></span>
<span><%= song.description %></span>
<a href="<%= song.download_url %>">Download</a>
</section>
<% end %>
</div>
μ΄ μμ λ μλ²½νμ§λ μμ΅λλ€. Controller μ½λλ₯Ό λ μ½κΈ° μ½κ² λ§λ€κ³ SQL νμ€ν μ½λλ₯Ό νΌνκ³ μΆλ€λ©΄ μ΄μ λΈλ‘κ·Έ(λ²μλ³Έ)λ₯Ό νμΈνμκΈ° λ°λλλ€. λν View λ μ΄μ΄μμ λ‘μ§μ μ μΈνλ©΄ λ€λ₯Έ μ¬λλ€μ΄ μ΄λ₯Ό κΈ°λ°μΌλ‘ ν΄κ²°μ± μ ꡬμΆνλ €λ κ°λ₯μ±μ΄ λμμ§λλ€.
Railsκ° μ 곡νλ κΈ°λ₯μ νμ©νκΈ°
μ¬κΈ°μλ μ§§κ² μ€λͺ νκ² μ΅λλ€. νλ μμν¬λ‘μ Ruby On Railsλ νΉν View λ΄λΆμ κΉλν Helperκ° λ§μ΄ ν¬ν¨λμ΄ μμ΅λλ€. μ΄ λ©μ§ μμ Helperλ₯Ό μ¬μ©νλ©΄ View λ μ΄μ΄λ₯Ό λΉ λ₯΄κ³ μ½κ² λΉλν μ μμ΅λλ€. Railsμ μ΄λ³΄μλΌλ©΄ erbνμΌ λ΄μ μ 체 HTMLμ μλμ κ°μ΄ μμ±νκ³ μΆμ μλ μμ΅λλ€:
# app/views/songs/new.html.erb
<form action="/songs" method="post">
<div class="field">
<label for="song_title">Title</label>
<input type="text" name="song[title]" id="song_title">
</div>
<div class="field">
<label for="song_description">Description</label>
<textarea name="song[description]" id="song_description"></textarea>
</div>
<div class="field">
<label for="song_download_url">Download URL</label>
<textarea name="song[download_url]" id="song_download_url"></textarea>
</div>
<input type="submit" name="commit" value="Create Song">
</form>
μ΄ HTMLμ μ¬μ©νλ©΄ μλ μ€ν¬λ¦°μ·κ³Ό κ°μ΄ μλ‘μ΄ λ Έλλ₯Ό λ±λ‘ν μ μλ Formμ μ»μμ μμ΅λλ€.

νμ§λ§ Railsλ₯Ό μ¬μ©νλ€λ©΄, Railsκ° κΈ°λ₯μ μ 곡ν΄μ£ΌκΈ° λλ¬Έμ μΌλ° HTMLμ μμ±ν νμλ μκ³ μμ±ν΄μλ μλ©λλ€. form_with view helperλ₯Ό μ¬μ©νλ©΄ HTMLμ μλμΌλ‘ μμ±ν΄ μ€λλ€. form_withμ Rails 5.1μ λμ λμμΌλ©° μΌλΆ μ¬μ©μμκ² μ΅μν form_tag λ° form_forλ₯Ό λ체νκΈ° μν κ²μ λλ€. form_withμ΄ μ΄λ»κ² μΆκ° μ½λ μμ±μ λμ΄μ£Όλμ§ μ΄ν΄λ³΄κ² μ΅λλ€.
<%= form_with(model: song, local: true) do |form| %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description %>
</div>
<div class="field">
<%= form.label :download_url do %>
Download URL
<% end %>
<%= form.text_area :download_url %>
</div>
<%= form.submit %>
<% end %>
βform_withβμ HTMLμ μμ±νλ κ² μΈμλ CSRF 곡격μ λ°©μ§νλ μΈμ¦ ν ν°λ μμ±ν©λλ€. λ°λΌμ κ±°μ λͺ¨λ κ²½μ°μ μ§μ λ ν¬νΌλ₯Ό μ¬μ©νλ κ²μ΄ λ μ’μΌλ©°, μ΄λ Rails νλ μμν¬μ μ μλν μ μκΈ° λλ¬Έμ λλ€. μΌλ° HTML Formμ μ μΆνλ €κ³ νλ©΄ μμ²κ³Ό ν¨κ» μ μΆλ μ ν¨ν ν ν°μ΄ μκΈ° λλ¬Έμ μ€ν¨ν©λλ€.
CSRF(Cross Site Request Forgery)λ?
μΉ μ΄ν리μΌμ΄μ μ·¨μ½μ μ€ νλλ‘, μ¬μ©μκ° μμ μ μμ§μλ λ€λ₯΄κ² 곡격μκ° μλν νμλ₯Ό νΉμ μΉμ¬μ΄νΈμ μμ²νκ² λ§λλ 곡격μ λλ€. μλ₯Ό λ€μ΄, μ¬μ©μκ° λ‘κ·ΈμΈν μνμμ νμ΄μ€λΆμ κΈμ μ°λ κ²μ²λΌ 보μ΄λ νΌμ± μ¬μ΄νΈμ μ μνλ©΄, 곡격μκ° λ§λ κ΄κ³ μ± κΈμ΄ νμ΄μ€λΆμ κ²μλ μ μμ΅λλ€. μ΄λ¬ν 곡격μ λ°©μ§νκΈ° μν΄, μλ²μμλ μμμ λμ κ°μ μμ±νμ¬ μΈμ μ μ μ₯νκ³ , μμ²λ§λ€ μ΄ κ°μ μ λ¬ν©λλ€. μλ²λ μμ²μ λ°μ λλ§λ€ μΈμ μ μ μ₯λ κ°κ³Ό μμ²μ μ λ¬λ κ°μ΄ μΌμΉνλμ§ κ²μ¦νμ¬, μμ‘° μμ²μ κ±°λΆν©λλ€. μ΄λ¬ν λμ κ°μ Security Tokenμ΄λΌκ³ λΆλ¦ λλ€.
βform_withβ, βlabelβ, βtext_areaβ, βsubmitβ ν¬νΌ μΈμλ Railsμλ View ν¬νΌκ° λ λ§μ΄ κΈ°λ³ΈμΌλ‘ μ 곡λ©λλ€. μ΄λ¬ν View ν¬νΌλ μ¬λ¬λΆλ€μ κ°λ° μΆμ λ μ½κ² λ§λ€μ΄ μ£ΌκΈ° μν΄ μ‘΄μ¬νλ―λ‘ λ μ μμλ¬μΌ ν©λλ€. μ¬μ€νμ€ νλλ βlink_toβ μ λλ€.
<%= link_to "Songs", songs_path %>
μλμ κ°μ΄ HTMLμ΄ μμ±λ©λλ€.
<a href="/songs">Songs</a>
κ° ν¬νΌμ λν΄μ μμΈν μ€λͺ νλ€ λ³΄λ©΄ μ΄κΈμ΄ λ무 κΈΈμ΄μ§κ³ , λͺ¨λ ν¬νΌλ₯Ό μ΄ν΄λ³΄λ κ²μ΄ μ€λμ μ£Όμ λ μλλ―λ‘ μμΈν μ€λͺ νμ§ μκ² μ΅λλ€. κΆκΈνμλ€λ©΄ **Rails Action View helpers guide**λ₯Ό μ°Έκ³ ν΄μ νμν ν¬νΌλ₯Ό μ ννλκ²μ΄ μ’μ΅λλ€.
View μ½λλ₯Ό μ¬μ¬μ©νκ³ μ 리νκΈ°
μλ²½ν μΉ μ ν리μΌμ΄μ μ΄ μλ€κ³ μμν΄ λ΄ μλ€. μλ²½ν μ¬μ© μ¬λ‘μλ if-elseλ¬Έμ΄ μκ³ Controllerμμ λ°μ΄ν°λ₯Ό κ°μ Έμ HTML νκ·Έ μ¬μ΄μ λ£λ μμν μ½λλ§ μμ΅λλ€. μ΄λ¬ν μ’ λ₯μ μ ν리μΌμ΄μ μ ν΄μ»€ν€μ΄λ κΏμμμλ μ‘΄μ¬ν μ μμ§λ§, μ€μ μ ν리μΌμ΄μ μλ Viewλ₯Ό λλλ§ν λ μ¬λ¬κ°μ§ λΆκΈ°μ μ‘°κ±΄μ΄ μ‘΄μ¬ν©λλ€.
νμ΄μ§μ μΌλΆλ₯Ό νμνλ λ‘μ§μ΄ λ무 볡μ‘ν΄μ§λ©΄ μ΄λ»κ² ν΄μΌ ν κΉμ? μΌλ°μ μΈ λλ΅μ μ΅μ JavaScript λΌμ΄λΈλ¬λ¦¬λ νλ μμν¬λ₯Ό μ¬μ©νμ¬ λ³΅μ‘ν κ²μ ꡬμΆνλ κ²μ λλ€. νμ§λ§ μ΄ κ²μλ¬Όμ Rails Viewμ κ΄ν κ²μ΄λ―λ‘ κ·Έ μμ μλ μ νμ§λ€μ μ΄ν΄λ³΄κ² μ΅λλ€.
After-Market (Custom) Helpers
Song μλμ CTA(call-to-action) λ²νΌμ νμνκ³ μΆλ€κ³ κ°μ ν΄λ³΄κ² μ΅λλ€. νμ§λ§ ν κ°μ§ μ μ± μ΄ μμ΅λλ€. Songμ λ€μ΄λ‘λ URLμ΄ μ‘΄μ¬ν μλ μκ³ μλμλ μμ΅λλ€. κ·Έλ λ€λ©΄ λ€μκ³Ό κ°μ΄ μ½λλ₯Ό μμ±νκ² λ κ²μ λλ€.
app/views/songs/show.html.erb
...
<div class="song-cta">
<% if @song.download_url %>
<%= link_to "Download", download_url %>
<% else %>
<%= link_to "Subscribe to artists updates",
artist_updates_path(@song.artist) %>
<% end %>
</div>
...
μμ μμμμ κ³ λ¦½λ νλ μ ν μ΄μ λ‘μ§μΌλ‘λ§ λ³Έλ€λ©΄ κ·Έλ κ² λμμ§ μμκ±° κ°μ΅λλ€. κ·Έμ΅Έ? νμ§λ§ μ΄λ¬ν μ‘°κ±΄λΆ λλλ§μ΄ λ§μμ§λ€λ©΄ μ½λμ κ°λ μ±μ΄ λ¨μ΄μ§λλ€. νΉν μ‘°κ±΄μ΄ λ§μΌλ©΄ μ΄λκ°μμ μ λλ‘ λλλ§ λμ§ μμ κ°λ₯μ±λ λμμ§λλ€.
μ΄λ¬ν λ¬Έμ λ₯Ό ν΄κ²°νλ ν κ°μ§ λ°©λ²μ λ³λμ ν¬νΌλ‘ μΆμΆνλ κ²μ λλ€. λ€ννλ Railsλ μ¬μ©μ μ μ ν¬νΌλ₯Ό μ½κ² μμ±ν μ μλ λ°©λ²μ μ 곡ν©λλ€. βapp/helpersβμμ λ€μκ³Ό κ°μ΄ βSongHelperβλ₯Ό λ§λ€μ μμ΅λλ€.
module SongsHelper
def song_cta_link
content_tag(:div, class: 'song-cta') do
if @song.download_url
link_to "Download", @song.download_url
else
link_to "Subscribe to artists updates",
artist_updates_path(@song.artist)
end
end
end
end
Songμ show νμ΄μ§λ₯Ό μ΄μ΄λ μ¬μ ν λμΌν κ²°κ³Όλ₯Ό μ»μμκ° μμ΅λλ€. νμ§λ§ μ΄ μμ λ₯Ό μ‘°κΈ λ κ°μ ν μ μμ΅λλ€. μμ μμ μμλ μΈμ€ν΄μ€ λ³μμΈ β@Songβμ μ¬μ©νμ΅λλ€. λ Έλκ° nilμΈ κ³³μμ μ΄ ν¬νΌλ₯Ό μ¬μ©νκΈ°λ‘ κ²°μ νλ€λ©΄ μ΄ λ©μλλ₯Ό μ¬μ©ν μκ° μμμλ μμ΅λλ€. λ°λΌμ μΈμ€ν΄μ€ λ³μ ννμ μΈλΆ μ’ μμ±μ μ°¨λ¨νκΈ° μν΄ λ€μκ³Ό κ°μ΄ ν¬νΌμκ² μΈμλ₯Ό μ λ¬ν μ μμ΅λλ€.
module SongsHelper
def song_cta_link(song)
content_tag(:div, class: 'song-cta') do
if song.download_url
link_to "Download", song.download_url
else
link_to "Subscribe to artists updates",
artist_updates_path(song.artist)
end
end
end
end
viewμμ μλμ κ°μ΄ μ¬μ©ν μ μμ΅λλ€
app/views/songs/show.html.erb
...
<%= song_cta_link(@song) %>
...
μ΄λ κ² νλ©΄ Viewμμ μ΄μ κ³Ό λμΌν κ²°κ³Όλ₯Ό μ»μ μ μμ΅λλ€. ν¬νΌλ₯Ό μ¬μ©ν λμ μ₯μ μ ν¬νΌμ λν ν μ€νΈλ₯Ό μμ±ν΄μ ν₯ν ν¬νΌμ κ΄λ ¨λ νκ·κ° λ°μνμ§ μλλ‘ ν μ μλ€λ μ μ λλ€. λ¨μ μ ν¬νΌκ° μ μμ μΌλ‘ μ μλλ―λ‘ μ± μ 체μμ ν¬νΌ μ΄λ¦μ΄ κ³ μ νμ§ νμΈν΄μΌ νλ€λ μ μ΄λΉλ€.
Rails μ¬μ©μ ν¬νΌλ₯Ό μμ±νλ κ²μ μ’μνμ§ μλ λ€λ©΄ draper gemμ μ¬μ©ν΄ View Modelν¨ν΄μ μ¬μ©ν΄λ³Όμλ μμ΅λλ€. μ¬κΈ°μμ μμ λ§μ View Model ν¨ν΄μ μ§μ λ§λ€μ μμΌλ©°, κ·Έλ κ² λ³΅μ‘νμ§ μμ΅λλ€. μΉμ±μ λ§ μμνλ κ²½μ°μλ μ¬μ©μ μ μ ν¬νΌλ₯Ό μμ±νμ¬ μ²μ²ν μμνκ³ λ¬Έμ κ° λ°μνλ©΄ λ€λ₯Έ ν΄κ²°μ± μΌλ‘ μ ννλ κ²μ΄ μ’μ΅λλ€.
DRY up Your Views
Railsλ₯Ό μμνλ©΄μ μ λ§ λ§μμ λ€μλ μ μ λ§ν¬μ μ μ½κ² μ 리ν μ μλ€λ μ μ΄μμ΅λλ€. Railsλ μ΄λμλ ν¬ν¨λ μ μλ μ¬μ¬μ© κ°λ₯ν μ½λ μ‘°κ°μΈ partialsλ₯Ό μμ±ν μ μλ κΈ°λ₯μ μ 곡ν©λλ€. μλ₯Ό λ€μ΄ μ¬λ¬ κ³³μμ Songμ λλλ§νκ³ μκ³ μ¬λ¬ νμΌμ λμΌν μ½λκ° μλ κ²½μ° Song partialμ λ§λλ κ²μ΄ μ’μ΅λλ€.
μλμ κ°μ΄ λ Έλλ₯Ό νμνλ€κ³ κ°μ ν΄λ³΄κ² μ΅λλ€.
# app/views/songs/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @song.title %>
</p>
<p>
<strong>Description:</strong>
<%= @song.description %>
</p>
<%= song_cta_link %>
<%= link_to 'Edit', edit_song_path(@song) %> |
<%= link_to 'Back', songs_path %>
νμ§λ§ λμΌν λ§ν¬μ μΌλ‘ λ€λ₯Έ νμ΄μ§μλ νμνκ³ μΆμμλ μμ΅λλ€. κ·Έλ λ€λ©΄ βapp/views/songs/_song.html.erbβμ κ°μ΄ μΈλλ°(_) μ λμ¬κ° λΆμ μ νμΌμ λ§λ€λ©΄ λ©λλ€.
# app/views/songs/_song.html.erb
<p>
<strong>Title:</strong>
<%= @song.title %>
</p>
<p>
<strong>Description:</strong>
<%= @song.description %>
</p>
<%= song_cta_link(@song) %>
κ·Έλ¦¬κ³ μ΄λμμλ μνλ κ³³μ Song partialμ μλμ κ°μ΄ μΆκ°νλ©΄ λ©λλ€.
...
<%= render "song" %>
...
β_Songβ partialμ μ‘΄μ¬ μ¬λΆλ₯Ό μλμΌλ‘ μ‘°νν νμ λλλ§μ ν©λλ€. μ¬μ©μ μ μ ν¬νΌλ₯Ό μ¬μ©ν μμ μ λ§μ°¬κ°μ§λ‘ partialμμ μΈμ€ν΄μ€ λ³μμ β@songβμ μ κ±°νλ κ²μ΄ κ°μ₯ μ’μ΅λλ€.
# app/views/songs/_song.html.erb
<p>
<strong>Title:</strong>
<%= song.title %>
</p>
<p>
<strong>Description:</strong>
<%= song.description %>
</p>
<%= song_cta_link(song) %>
κ·Έλ° λ€μ songμ λ³μλ₯Ό partialμ μ λ¬νμ¬ μ¬μ¬μ©μ±μ λμ΄κ³ λ€λ₯Έ κ³³μ ν¬ν¨μν€κΈ°μ μ ν©νλλ‘ λ§λ€μ΄μΌ ν©λλ€.
...
<%= render "song", song: @song %>
...
λ§μ§λ§ μκ°
μ΄ ν¬μ€ν μ μ¬κΈ°κΉμ§μ λλ€. μμ½νμλ©΄ Rails View μμμμ λ³Ό μ μλ λͺ κ°μ§ ν¨ν΄κ³Ό μν°ν¨ν΄μ μ΄ν΄λ΄€μ΅λλ€. λ€μμ λͺ κ°μ§ μμ μ λλ€:
- UIμμ 볡μ‘ν λ‘μ§ νΌνκΈ°(Viewκ° λ§μ Powerliftingμ μννμ§ μλλ‘ λ§λ€κΈ°.)
- Railsκ° View ν¬νΌμ κ΄λ ¨λ κΈ°λ³Έμ μΌλ‘ μ 곡νλ κΈ°λ₯μ λν΄ μμ보μΈμ.
- μ¬μ©μ μ§μ ν¬νΌ λ° partial μ½λλ₯Ό ꡬ쑰ννκ³ μ¬μ¬μ© νμΈμ.
- μΈμ€ν΄μ€ λ³μμ λ무 λ§μ΄ μμ‘΄νμ§ λ§μΈμ.
λ€μ κ²μλ¬Όμμλ μλΉν μ§μ λΆν΄μ§μ μλ Rails Controller ν¨ν΄κ³Ό μν° ν¨ν΄μ λν΄ λ€λ£° κ²μ λλ€. κΈ°λν΄μ£ΌμΈμ.
λ€μ μκ°κΉμ§ νμ΄ν !
PS. Ruby Magic κ²μλ¬Όμ΄ λ³΄λλλ λλ‘ μ½κ³ μΆμΌμλ©΄ Ruby Magic λ΄μ€λ ν°λ₯Ό ꡬλ νμκ³ λ¨ νλμ κ²μλ¬Όλ λμΉμ§ λ§μΈμ!