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

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

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

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

Ruby

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

Q&A

解決済

1回答

1382閲覧

いいね!の通知機能でいいねしたマイクロポストを表示したい。

messy1019

総合スコア18

Ruby on Rails 5

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

Ruby

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

0グッド

0クリップ

投稿2018/09/14 10:59

ツイッターのような機能でユーザーがいいね!したら、いいねされたマイクロポストの作成者に通知が飛ぶ機能を追加しました。

そこにどのマイクロポストのいいねされたか識別するべく、通知分の下にマイクロポストの一部を表示したいと考えています。

●notifications_controller.rb

def index if current_user.notifications.present? @notifications = current_user.notifications @notifications.update read: true @notifieds = @notifications.where(notified_type: "いいね") @microposts = @notifieds.map{|notified| notified.micropost} end end

コントローラーで通知されるデータを取得し、viewにわたします。
●index.html.haml

- provide(:title, "通知") - if logged_in? .jumbotron2.jumbotron-extend .container.jumbotron-container .row %aside.user-information.col-md-4 %section.user-info = render 'shared/user_info' %section.stats1 = render 'shared/stats' .micropost-list1.col-md-6 - if current_user.notifications.any? .action-list %h4 通知 %ul - if @notifications.any? - @microposts.each do |micropost| = render current_user.notifications.order('created_at DESC'), locals: {micropost: micropost }

ここで、@micropostsをeachで崩して、renderにmicropostをわたしています。
●_notification.html.haml

%li - if notification.notified_type == "いいね" = link_to gravatar_for(notification.notified_by, size: 35), notification.notified_by .notifications = link_to "#{notification.notified_by.nickname}" %span.notifications-content さんがあなたの投稿にいいねしました %span.timestamp ・&nbsp;#{time_ago_in_words(notification.created_at)} %p.notifications-micropost = raw(micropost.content.gsub(/\n/, '<br>')) = image_tag micropost.picture.url if micropost.picture? - elsif notification.notified_type == "フォロー" = link_to gravatar_for(notification.notified_by, size: 35), notification.notified_by .notifications = link_to "#{notification.notified_by.nickname}" %span.notifications-content さんがあなたをフォローしました %span.timestamp ・&nbsp;#{time_ago_in_words(notification.created_at)}

渡されたmicropostをこのviewで表示したいと思います。
現在、エラーが出ている箇所は以下となります。

= raw(micropost.content.gsub(/\n/, '<br>'))

エラー内容

undefined local variable or method `micropost' for #<#<Class:0x007fa390589248>:0x007fa38d625538> Did you mean? @microposts

おそらく、うまくmicropostを渡せていないことが原因かと思っています。
もし、お分かりになる方がいらっしゃいましたら、教えていただけると幸いです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

部分テンプレートにlocals:を使って変数を渡す時は partial:もつけないとエラー

念の為、railsのドキュメントも調べましたが

render

第一引数がハッシュでない場合はpartialとして扱い、第2引数をlocalsとして扱う(適当訳)

との事で、locals:を使うときにはpartial:を第1引数につける必要があるようです。

投稿2018/09/14 13:57

asm

総合スコア15147

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

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

messy1019

2018/09/15 08:49

ご回答いただきましてありがとうございます! 結局、partialは使わなかったのですが、asmさんのご指摘でヒントをいただき解決することができました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問