前提・実現したいこと
投稿一覧ページにいいね機能をつけたい
発生している問題・エラーメッセージ
現在、投稿詳細ページ(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]
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。