1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

パーシャルビューの呼び出し

Last updated at Posted at 2019-07-23

renderでパーシャルビューファイルの呼び出しが、よく理解できなかったので、おさらい。
コードの中身はシンプルにしました。

index.html.rb
<div class="contents row">
  <%= render @tweets %>
  <%= paginate(@tweets) %>
</div>

_tweet.html.rb
<div class="content__post" style="background-image: url(<%= tweet.image %>);">
    <%= simple_format(tweet.text) %>
</div>

ツイート一覧(index)で、ツイートを並べる部分をパーシャルにして、renderで描画しているのですが、なぜ_tweet.html.rbを自動で認識してくれているのかが不明でしたが、

ざっくり説明すると、<%= render @tweets %>こう省略して記述ができ、Railsがrenderを使っているならpartialだろうな→同じフォルダの改装にtweetの名前をしたpartialないかなと勝手に探してくれるみたいです。

index.html.rb
<%= @tweets.each do |tweet| %>
  <%= render :partial => "tweet", :locals => { tweet: tweet } %>
<% end %>

このようにrenderを省略せずに書くと、tweetのpartialとして_tweet.html.erbを呼び出しているし、その中でtweet:は@tweetsの中から一つづつ呼び出したtweetをさしていることがわかりました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?