###前提・実現したいこと
他モデルへのshowページへ飛べるように条件分岐をしたいです。
例えば、あるユーザーがUserとしてログインしていたとします。
そこから、Shopユーザーのshowページへ飛べるというイメージです。
Userログイン→Shopindexページ→Shopshowページ
###発生している問題・エラーメッセージ
株式会社ソラコムというshowページリンクをクリックすると、
undefined method `email' for nil:NilClass
showページのコードです。
下から6行目の_micropostパーシャルに問題があります。
【shops/show.html.erb】
<% provide(:title, @shop.shopname) %> <div class="row"> <!-- ページ左 --> <aside class="col-md-4"> <section class="shop_info"> <h1> <!-- アイコン --> <%= gravatar_to @shop %> <!-- ユーザー名 --> <%= @shop.shopname %> </h1> </section> <section class="stats"> <!-- follow,followerリンク --> <%= render 'shared/stats'%> </section> </aside> <!-- ページ右 --> <div class="col-md-8"> <!-- follow(shop-to-shop)ボタン --> <%= render 'friend_form' if shop_signed_in? %> <!-- follow(user-to-shop)ボタン --> <% if user_signed_in? %> <div id="follow_form"> <%= render 'associates/associate_links'%> </div> <% end %> <% if @shop.microposts.any? %> <!-- 投稿数 --> <h3>Microposts (<%= @shop.microposts.count %>)</h3> <ol class="microposts"> <!-- 投稿一覧 --> <%= render @microposts %> </ol> <%= will_paginate @microposts %> <% end %> </div> </div>
_micropostパーシャルです。
4行目のgaravatar_forがエラーとなっています。
【_micropost.html.erb】
<li id="micropost-<%= micropost.id %>"> <% if user_signed_in? %> <!-- ユーザーアイコン --> <%= link_to gravatar_for(micropost.user, size: 50), micropost.user %> <!-- ユーザー名 --> <span class="user"><%= link_to micropost.user.username, micropost.user %></span> <span class="content"> <!-- 投稿文 --> <%= micropost.content %> <!-- 投稿写真 --> <%= image_tag micropost.picture.url if micropost.picture? %> </span> <!-- 投稿時間 --> <span class="timestamp"> Posted <%= time_ago_in_words(micropost.created_at) %> ago. </span> <!-- 削除リンク --> <% if current_user?(micropost.user) %> <%= link_to "delete", micropost, method: :delete, data: { confirm: "You sure?" } %> <% end %> <!-- お気に入り登録リンク --> <% if !current_user?(micropost.user) %> <div id="follow_form"> <%= render 'favorites/favorite_links', micropost: micropost %> </div> <% end %> <% elsif shop_signed_in? %> <!-- ショップアイコン --> <%= link_to gravatar_to(micropost.shop, size: 50), micropost.shop %> <!-- ショップ名 --> <span class="shop"><%= link_to micropost.shop.shopname, micropost.shop %></span> <span class="content"> <!-- 投稿文 --> <%= micropost.content %> <!-- 投稿写真 --> <%= image_tag micropost.picture.url if micropost.picture? %> </span> <!-- 投稿時間 --> <span class="timestamp"> Posted <%= time_ago_in_words(micropost.created_at) %> ago. </span> <!-- 削除リンク --> <% if current_shop?(micropost.shop) %> <%= link_to "delete", micropost, method: :delete, data: { confirm: "You sure?" } %> <% end %> <!-- いいね!リンク --> <% if !current_shop?(micropost.shop) %> <div id="follow_form"> <%= render 'likes/like_links', micropost: micropost %> </div> <% end %> <% end %> </li>
gravatarメソッドです。
コードは以下です。
この4行目、emailがnilだとエラーメッセージがでます。。。
【users_helper】
module UsersHelper def gravatar_for(user, options = { size: 50 } ) size = options[:size] gravatar_id = Digest::MD5::hexdigest(user.email.downcase) gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}" image_tag(gravatar_url, alt: user.username, class: "gravatar") end def current_user?(user) user == current_user end end
_micropostパーシャルの時にshop専用に用意したメソッドgravatar_toメソッドを使えばこのような問題は起こらないと思うのです。
しかし、_micropostパーシャルの条件分岐がuser_signed_in?しかないので、garavatar_forを使わざるを得ない状況に陥ってしまいます。
そこで、この問題を解決できる条件分岐はないかお聞きしたいです!
よろしくおねがいします。
回答2件
あなたの回答
tips
プレビュー