Ruby on Railsで下記のようなエラーが発生したのですが解決策がわかりません。
わかるかたご教授お願いします。
NoMethodError in Users#likes
undefined method `each' for nil:NilClass
恐らくlikes.html.erbのどこかがおかしいのだと思います
_microposts.html.erb
microposts.html.erb
1<ul class="list-unstyled"> 2 <% microposts.each do |micropost| %> 3 <li class="media mb-3"> 4 <img class="mr-2 rounded" src="<%= gravatar_url(micropost.user, { size: 50 }) %>" alt=""> 5 <div class="media-body"> 6 <div> 7 <%= link_to micropost.user.name, user_path(micropost.user) %><span class="text-muted"> posted at <%= micropost.created_at %></span> 8 </div> 9 <div> 10 <p class="mb-0"><%= micropost.content %></p> 11 </div> 12 </br> 13 14 <div> 15 <% if current_user.checksub?(micropost) %> 16 <%= form_with(model: current_user.favorites.find_by(micropost_id: micropost.id), local: true, method: :delete) do |f| %> 17 <%= hidden_field_tag :micropost_id, micropost.id %> 18 <%= f.submit 'お気に入り解除', class: 'btn btn-danger' %> 19 <% end %> 20 <% else %> 21 <%= form_with(model: current_user.favorites.build, local: true) do |f| %> 22 <%= hidden_field_tag :micropost_id, micropost.id %> 23 <%= f.submit 'お気に入り登録', class: 'btn btn-primary' %> 24 <% end %> 25 <% end %> 26 </div> 27 </br> 28 29 <div> 30 <% if current_user == micropost.user %> 31 <%= link_to "Delete", micropost, method: :delete, data: { confirm: "You sure?" }, class: "btn btn-danger btn-sm" %> 32 <% end %> 33 </div> 34 </div> 35 </li> 36 <%= paginate microposts %> 37</ul> 38 <% end %>
likes.html.erb
<div class="row"> <aside class="col-sm-4"> <div class="card"> <div class="card-header"> <h3 class="card-title"><%= @user.name %></h3> </div> <div class="card-body"> <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt=""> </div> </div> <%= render 'microposts/microposts', microposts: @microposts %> </aside> <div class="col-sm-8"> <ul class="nav nav-tabs nav-justified mb-3"> <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts <span class="badge badge-secondary"><%= @count_microposts %></span></a></li> <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings <span class="badge badge-secondary"><%= @count_followings %></span></a></li> <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers <span class="badge badge-secondary"><%= @count_followers %></span></a></li> <li class="nav-item"><a href="<%= likes_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(likes_user_path(@user)) %>">Likes <span class="badge badge-secondary"><%= @count_likes %></span></a></li> </ul> <%= render 'users', users: @favorites %> </div> </div>
microposts_controller.rb
class MicropostsController < ApplicationController before_action :require_user_logged_in before_action :correct_user, only: [:destroy] def create @micropost = current_user.microposts.build(micropost_params) if @micropost.save flash[:success] = "メッセージを投稿しました。" redirect_to root_url else @microposts = current_user.feed_microposts.order(id: :desc).page(params[:page]) flash.now[:danger] = "メッセージの投稿に失敗しました。" render "toppages/index" end end def destroy @micropost.destroy flash[:success] = "メッセージを削除しました。" redirect_back(fallback_location: root_path) end private def micropost_params params.require(:micropost).permit(:content) end def correct_user @micropost = current_user.microposts.find_by(id: params[:id]) unless @micropost redirect_to root_url end end end
users_controller.rb
class UsersController < ApplicationController before_action :require_user_logged_in, only: [:index, :show, :followings, :followers, :likes] def index @users = User.order(id: :desc).page(params[:page]).per(25) end def show @user = User.find(params[:id]) @microposts = @user.microposts.order(id: :desc).page(params[:page]) counts(@user) end def new @user = User.new end def create @user = User.new(user_params) if @user.save flash[:success] = 'ユーザを登録しました。' redirect_to @user else flash.now[:danger] = 'ユーザの登録に失敗しました。' render :new end end def followings @user = User.find(params[:id]) @followings = @user.followings.page(params[:page]) counts(@user) end def followers @user = User.find(params[:id]) @followers = @user.followers.page(params[:page]) counts(@user) end def likes @user = User.find(params[:id]) @likes = @user.likes.page(params[:page]) counts(@user) end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end end