2
2

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.

<%= render @hogehoges %>でなぜ/hogehoges/_hogehoge.html.erbを使ってくれるのか

Posted at

間違っていたら教えてほしいです。

タイトルの通りRailsは省略記法やら使うファイル名を勝手に気を利かせてくれるところが便利でわかりにくいところだと常々おもっております。今回もその愚痴です。

<%= render ほげほげ %>

HTMLを描写してくれるERbの書き方で

<%= render 'XXX' %>

というのがあります。'XXX'の部分に/viewディレクトリにあるパーシャル(テンプレートみたいなもん)を読み込んで描写してくれる、という1文です。

<%= render 'dir/sample' %>

と書いてあれば、"/app/view/dir/_sample.html.erb"を読み込んで"<%= render"を記載した箇所に描写してくれるということです。

ここがわからん

で、Ruby on Railsチュートリアルの「13.3.3 フィードの原型」に下記の記載方法が登場しました。

/app/views/shared/_feed.html.erb
<% if @feed_items.any? %>
  <ol class="microposts">
    <%= render @feed_items %>
  </ol>
  <%= will_paginate @feed_items %>
<% end %>

これです。

<%= render @feed_items %>

この書き方は何なのだろう、と。チュートリアルの説明文を呼んでみると、

@feed_itemsの各要素がMicropostクラスを持っていたため、RailsはMicropostのパーシャルを呼び出すことができました。このように、Railsは対応する名前のパーシャルを、渡されたリソースのディレクトリ内から探しにいくことができます。

ちょっと何言っているかわからない。そもそも@feed_itemsはStaticPagesコントローラで下記のように定義されています。

/app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
  def home
    if logged_in? #Cookieを見てログインしている状態(true)であることを確認。今回ほぼ無関係。
      @micropost  = current_user.microposts.build
      @feed_items = current_user.feed.paginate(page: params[:page])
      #current_userには現在ログイン中のユーザが代入されている(=User.find(params[:id]))
    end
  end
end

で、この.feedというメソッドがUserモデルで定義されています。

/app/models/user.rb
class User < ApplicationRecord
・・・
  def feed
    Micropost.where("user_id=?", id)
  end
・・・
end

勝手にこんなふうに解釈していますよ

つまり、.feedというメソッドを使うとMicropostモデル(Micropostクラス)に所属するオブジェクトを参照することとなるのでモデル名と同じ名称のディレクトリの/app/view/microposts/に入っているパーシャルを自動的に選択してくれる、ということです。
また、.feedで参照した値はMicropostsテーブルのレコードが配列変数の形で代入されたものとなります。
なので、.feedで参照した値を代入した@feed_itemsも同様です。
結果として下記の動作となります。
・Micropostsテーブルの値が代入された配列なので、Railsの仕様で自動的に/app/view/microposts/のパーシャルを使う
・使うパーシャルのファイル名は、Railsの仕様でMicropostsの単数形がつけられたファイル名を使う
・代入されたレコードの数だけ<%= render 'microposts/micropost" %>を繰り返す
よって、

<%= render @feed_items %>

の1行は下記と同義となります。

<%= render 'microposts/micropost' %> #@feed_itemsの1件目
<%= render 'microposts/micropost' %> #@feed_itemsの2件目
<%= render 'microposts/micropost' %> #@feed_itemsの3件目
・
・
・
<%= render 'microposts/micropost' %> #@feed_itemsのn件目
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?