前提・実現したいこと
railsでお気に入り機能の一覧表示。
発生している問題・エラーメッセージ
お気に入り一覧を表示させたいのですが、gravatar_urlメソッドでエラーが出てしまいます。followimgsとfollowersの一覧表示を開いてもこのようなエラーは出ないのですがfavorites(お気に入り)の一覧表示をしようとした時だけこのエラーが出ます。
エラーメッセージ ActionView::Template::Error (undefined method `email' for #<Favorite:0x00007f5ba4ac5038>): 2: <ul class="list-unstyled"> 3: <% users.each do |user| %> 4: <li class="media"> 5: <img class="mr-2 rounded" src="<%= gravatar_url(user, { size: 50}) %>" alt=""> 6: <div class="media-body"> 7: <div> 8: <%= user.name %> app/helpers/users_helper.rb:3:in `gravatar_url' app/views/users/_users.html.erb:5:in `block in _app_views_users__users_html_erb___265283563848892159_70015790846520' app/views/users/_users.html.erb:3:in `_app_views_users__users_html_erb___265283563848892159_70015790846520' app/views/users/favorites.html.erb:24:in `_app_views_users_favorites_html_erb___2237680306968847002_70015791066860'
favorites.html.erb
ruby
1 2 3<div class="row"> 4 <aside class="col-sm-4"> 5 <div class="card"> 6 <div class="card-header"> 7 <h3 class="card-title"><%= @user.name %></h3> 8 </div> 9 <div class="card-body"> 10 <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt=""> 11 </div> 12 </div> 13 <%= render 'relationships/follow_button', user: @user %> 14 </aside> 15 <div class="col-sm-8"> 16 <ul class="nav nav-tabs nav-justified mb-3"> 17 <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts 18 <span class="badge badge-secondary"><%= @count_microposts %></span></a></li> 19 <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings 20 <span class="badge badge-secondary"><%= @count_followings %></span></a></li> 21 <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers 22 <span class="badge badge-secondary"><%= @count_followers %></span></a></li> 23 <li class="nav-item"><a href="<%= favorites_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(favorites_user_path(@user)) %>">Favorites 24 <span class="badge badge-secondary"><%= @count_favorites %></span></a></li> 25 </ul> 26 <%= render 'users', users: @favorites %> 27 </div> 28</div> 29
followings.html.erb
ruby
1<div class="row"> 2 <aside class="col-sm-4"> 3 <div class="card"> 4 <div class="card-header"> 5 <h3 class="card-title"><%= @user.name %></h3> 6 </div> 7 <div class="card-body"> 8 <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt=""> 9 </div> 10 </div> 11 <%= render 'relationships/follow_button', user: @user %> 12 13 </aside> 14 <div class="col-sm-8"> 15 <ul class="nav nav-tabs nav-justified mb-3"> 16 <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts 17 <span class="badge badge-secondary"><%= @count_microposts %></span></a></li> 18 <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings 19 <span class="badge badge-secondary"><%= @count_followings %></span></a></li> 20 <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers 21 <span class="badge badge-secondary"><%= @count_followers %></span></a></li> 22 <li class="nav-item"><a href="<%= favorites_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(favorites_user_path(@user)) %>">Favorites 23 <span class="badge badge-secondary"><%= @count_favorites %></span></a></li> 24 </ul> 25 <%= render 'users', users: @followings %> 26 </div> 27</div>
followers.html.erb
ruby
1<div class="row"> 2 <aside class="col-sm-4"> 3 <div class="card"> 4 <div class="card-header"> 5 <h3 class="card-title"><%= @user.name %></h3> 6 </div> 7 <div class="card-body"> 8 <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt=""> 9 </div> 10 </div> 11 <%= render 'relationships/follow_button', user: @user %> 12 </aside> 13 <div class="col-sm-8"> 14 <ul class="nav nav-tabs nav-justified mb-3"> 15 <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts 16 <span class="badge badge-secondary"><%= @count_microposts %></span></a></li> 17 <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings 18 <span class="badge badge-secondary"><%= @count_followings %></span></a></li> 19 <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers 20 <span class="badge badge-secondary"><%= @count_followers %></span></a></li> 21 <li class="nav-item"><a href="<%= favorites_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(favorites_user_path(@user)) %>">Favorites 22 <span class="badge badge-secondary"><%= @count_favorites %></span></a></li> 23 </ul> 24 <%= render 'users', users: @followers %> 25 </div> 26</div>
_users.html.erb
ruby
1<% if users.any? %> 2 <ul class="list-unstyled"> 3 <% users.each do |user| %> 4 <li class="media"> 5 <img class="mr-2 rounded" src="<%= gravatar_url(user, { size: 50}) %>" alt=""> 6 <div class="media-body"> 7 <div> 8 <%= user.name %> 9 </div> 10 <div> 11 <p><%= link_to 'View profile', user_path(user) %></p> 12 </div> 13 </div> 14 </li> 15 <% end %> 16 </ul> 17 <%= paginate users %> 18<% end %>
users_helper_rb
ruby
1module UsersHelper 2 def gravatar_url(user, options = { size: 80}) 3 gravatar_id = Digest::MD5::hexdigest(user.email.downcase) 4 size = options[:size] 5 "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}" 6 end 7end 8
試したこと
誤字など疑ったんですが、それも問題ありませんでした。
補足情報(FW/ツールのバージョンなど)
ruby on rails
長文失礼しました。3日考えたんですが解決できませんでした。ご協力お願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/02 06:36
2021/04/02 09:08