ツイッターのような機能でユーザーがいいね!したら、いいねされたマイクロポストの作成者に通知が飛ぶ機能を追加しました。
そこにどのマイクロポストのいいねされたか識別するべく、通知分の下にマイクロポストの一部を表示したいと考えています。
●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 ・ #{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 ・ #{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を渡せていないことが原因かと思っています。
もし、お分かりになる方がいらっしゃいましたら、教えていただけると幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/15 08:49