質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

1001閲覧

Rails:通知機能実装時の、Viewの記述方法について

1998Ttf

総合スコア6

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2021/08/10 22:56

##通知機能の実装についての単純な質問

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

'_notificationss.html.erb`というfileが 「こちらのView」と同じ場所にあるのですね?
でしたら 場所指定は不要です。
app/views/some/dir/nantoka.html.erb に 場所不指定の Render が有る場合、app/views/some/dir/ app/views/some app/views/application
の順に探します。
(ちょっと不正確ですが、標準的な controller、routes.rb の使い方をしていれば、このとおりです)

投稿2021/08/11 00:40

winterboum

総合スコア23416

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

1998Ttf

2021/08/11 01:35

ご回答ありがとうございます!その通りでございます。 同フォルダ上に、レンダーファイル(アンダースコアで始まるファイル)と、大元のファイルのみであれば 場所指定は不要なのですね! 大変助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問