LoginSignup
kmjooh
@kmjooh (Masayuki Kamijo)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

【rails5】パーシャルが表示されません。解決方法を教えてください。

解決したいこと

レシピサイトを作成しておりまして、ユーザーページにそのユーザーが投稿したレシピを一覧表示させたいです。
レシピ一覧表示のビューはパーシャルで別ファイルに作成しておりますが、それが表示されないので、解決方法を教えてください。
初歩的なミスかもしれませんが、色々と試してみましたが上手くいきませんでしたので、ご教示お願いします。

Image from Gyazo

今回該当するUserとRecipeテーブルのカラムは以下になります。
Image from Gyazo

該当するソースコード

users/show.html.erb
  <ul class="nav nav-tabs nav-justified my-3">
    <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">投稿レシピ <span class="badge badge-secondary"><%= @count_recipes %></span></a></li>
    <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">フォロー <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)) %>">フォロワー <span class="badge badge-secondary"><%= @count_followers %></span></a></li>
    <li class="nav-item"><a href="<%= favorites_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(favorites_user_path(@user)) %>">お気に入り <span class="badge badge-secondary"><%= @count_favorites %></span></a></li>
  </ul>

  <%= render 'recipes', recipes: @recipes %>
</div>

このshow.html.erbよりrenderして以下の_recipes.html.erbに飛ぶはずなのですが、表示されません。

users/_recipes.html.erb
<% if recipes.any? %>
  <ul class="list-unstyled">
    <% recipes.each do |recipe| %>
      <li>
        <%= image_tag recipe.image.url, size: "80x80" %>
        <%= recipe.title %>
      </li>
    <% end %>
  </ul>
  <%= paginate recipes %>
<% end %>

Userコントローラーは以下になります。

users_controller.rb
def show
    @user = User.find(params[:id])
    @recipes = @user.recipes.order(id: :desc).page(params[:page])
    counts(@user)
  end

宜しくお願いします。

0

1Answer

_recipes.html.erbが読み込めてないのか
@recipesが読み込めてないのかの切り分けができてないので

_recipes.html.erbに無条件に表示される文字列を含めてみるといいです。

0

Comments

  1. @kmjooh

    Questioner
    ありがとうございます!
    上記コメントに記載させて頂きましたが、無事解決しました。

Your answer might help someone💌