##通知機能の実装についての単純な質問
Rails初心者です。どうぞよろしくお願いいたします。
通知機能の実装時においての、Viewの記述について回答をいただきたく、質問させていただきました。
##通知画面で使用しているrenderの書き方の意味を理解できていない
下記のような形で、通知機能自体は実装しているのですが、renderの書き方でなぜ情報が取得できているのかあまり理解できていないです。
↓こちらのViewで、<%= render notifications %>となっているところの理解ができていないです。
<div class="container all"> <div class="row"> <div class="col-xs-12 col-ms-12 col-md-12 col-lg-12"> <% notifications = @notifications.includes(:visitor).includes(:visited).includes(:log).includes(:room).where.not(visitor_id: current_user.id) %> <% if notifications.exists? %> <%= render notifications %> <div class="d-flex justify-content-center"> <p><%= paginate @notifications %></p> </div> <% else %> <p>通知はありません</p> <% end %> </div> </div> </div>
本来ならば、render時にファイル先の指定をしなければならないところを、その上のローカル変数のみでレンダリングできているのですが
この二つのファイルは、Viewフォルダの「notifications」というフォルダの中にあります。ファイルはレンダリングのファイルと上記のファイルしかありません。
renderする際、レンダリングするファイルが同じフォルダの階層にいた場合、ファイル先指定は不要になるのでしょうか?
どうぞよろしくお願いいたします。
###ちなみに以下のコードは
・レンダー先の内容
・コントローラの内容
になります。ご教授お願いいたします。
<% visitor = notification.visitor %> <% visited = notification.visited %> <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 offset-xs-3 offset-sm-3 offset-md-3 offset-lg-3 mx-auto"> <table class="table table-border-bottom form-inline"> <tbody> <tr> <td> <%= link_to user_path(visitor), class: "text-dark" do %><strong><%= visitor.nick_name %>さん</strong>が<% end %> <% case notification.action %> <% when 'follow' then %> <%= "あなたをフォローしました" %> <% when 'favorite' then %> <%= link_to 'あなたの投稿', notification.log, class: "text-dark", style: "font-weight: bold;" %><%= "にいいねしました" %> <% when 'comment' then %> <% if notification.log.user_id == visited.id %> <%= link_to "あなたの投稿", notification.log, class: "text-dark", style: "font-weight: bold;" %> <% else %> <%= link_to user_path(visited.id), class: "text-dark" do %><%= notification.log.user.nick_name + "さんの" %><% end %><%= link_to "投稿", notification.log, class: "text-dark", style: "font-weight: bold;" %> <% end %> <span> <%= "にコメントしました" %> <p class="text-secondary"><%= LogComment.find_by(id: notification.log_comment_id).comment %></p> </span> <% else %> <%= link_to "あなたにメッセージ", notification.room, class: "text-dark", style: "font-weight: bold;" %> <span> <%= "を送信しました" %> <p class="text-secondary"><%= Message.find_by(id: notification.message_id).content %></p> </span> <% end %> <small class="text-right text-muted"><%= time_ago_in_words(notification.created_at).upcase %> </td> </tr> </tbody> </table> </div>
class NotificationsController < ApplicationController before_action :authenticate_user! def index @notifications = current_user.passive_notifications.page(params[:page]).per(10) @notifications.where(checked: false).each do |notification| notification.update_attributes(checked: true) end end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/11 01:35