質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1554閲覧

【Ruby on Rails】投稿一覧ページにいいね機能をつけたい

is02

総合スコア17

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/01/06 07:05

編集2020/01/06 08:48

前提・実現したいこと

投稿一覧ページにいいね機能をつけたい

発生している問題・エラーメッセージ

現在、投稿詳細ページ(show)にいいね機能を実装しております。
イメージ説明

投稿一覧ページ(index)に下記のソースコードを入力するとこのようなエラーが表示されます。
post_images/index.html.erb

<section class="sct-color-1 slice"> <div class="container-fluid masonry-container"> <div class="row"> <div class="col-md-8 col-sm-offset-2"> <div class="masonry-wrapper-cols"> <div class="masonry-gutter"></div> <% @post_images.each do |post_image| %> <div class="masonry-block"> <div class="block block-image v1"> <div class="block-image"> <div class="view view-first"> <%= link_to post_image_path(post_image.id) do %> <%= attachment_image_tag post_image, :image %> <% end %> </div> </div> <div class="block-content"> <%= attachment_image_tag post_image.user, :profile_image, size: "100x100", fallback: "no_image.jpg", class:"img-circle pull-left profile-thumb" %> <h3 class="block-title"> <%= post_image.image_name %> </h3> <ul class="inline-meta"> <li>By <%= post_image.user.name %> </li> <li> <%= link_to "#{post_image.post_comments.count} コメント", post_image_path(post_image.id) %> </li> <% if @post_image.favorited_by?(current_user) %> <li> <%= link_to post_image_favorites_path(@post_image), method: :delete do %> <i class="fa fa-star" aria-hidden="true" style="color: orange; font-size: 15px;"></i> <%= @post_image.favorites.count %> ファイト <% end %> </li> <% else %> <li> <%= link_to post_image_favorites_path(@post_image), method: :post do %> <i class="fa fa-star" aria-hidden="true" style="font-size: 15px;"></i> <%= @post_image.favorites.count %> ファイト <% end %> </li> <% end %> </ul> </div> </div> </div> <% end %> <%= paginate @post_images, class: "paginate" %> </div> </div> </div> </div> </section>

エラー内容
イメージ説明

ソースコード

post_image.rb(model)

class PostImage < ApplicationRecord belongs_to :user attachment :image has_many :post_comments, dependent: :destroy has_many :favorites, dependent: :destroy validates :image_name, presence: true validates :image, presence: true def favorited_by?(user) favorites.where(user_id: user.id).exists? end end

post_images_controller.rb(controller)

class PostImagesController < ApplicationController def new @post_image = PostImage.new end def create @post_image = PostImage.new(post_image_params) @post_image.user_id = current_user.id if @post_image.save redirect_to post_images_path else render :new end end def index @post_images = PostImage.page(params[:page]).reverse_order end def show @post_image = PostImage.find(params[:id]) @post_comment = PostComment.new end def destroy @post_image = PostImage.find(params[:id]) @post_image.destroy redirect_to post_images_path end private def post_image_params params.require(:post_image).permit(:image_name, :image, :caption) end end

補足情報

定義されていないということは理解できるんですが、どう直せば表示されるのかがわかりません。
ご教示いただけると幸いです。

追記

@post_imagesをpost_imageにしたら投稿一覧にいいねボタンが表示されるようになりました。

新たなエラーとして、いいねを押すと詳細ページに飛んでしまいます。
link_to post_image_favorites_path(@post_image)
にしているためだと思われますが、indexのルートであるpost_images_pathと書いてもroutes errorが表示されてしまいます。
favorites_controller.rb(controller)

class FavoritesController < ApplicationController def create post_image = PostImage.find(params[:post_image_id]) favorite = current_user.favorites.new(post_image_id: post_image.id) favorite.save redirect_to post_image_path(post_image) end def destroy post_image = PostImage.find(params[:post_image_id]) favorite = current_user.favorites.find_by(post_image_id: post_image.id) favorite.destroy redirect_to post_image_path(post_image) end end

Rails 5.2.4.1
ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-linux-gnu]

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

@post_imagesをpost_imageに変更したら投稿一覧にいいね機能の表示をすることができました。
原因はindex.html.erbでは<% @post_images.each do |post_image| %>のように@post_imagesをpost_imageという変数に格納していたため。

投稿2020/01/06 14:28

is02

総合スコア17

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問