前提・実現したいこと
前提:いいねボタンは投稿一覧ページ、投稿詳細ページに実装済み
実現したいこと:いいねボタンを押しても投稿一覧ページに留まりたいが、カウントはしたい。
投稿詳細ページは変わらずいいね機能を実装したい
投稿一覧ページ:post_images/index.html.erb
発生している問題・エラーメッセージ
現状:いいねボタンを押すと投稿詳細ページに飛んでしまう、カウントはする。
該当のソースコード
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>
favorites_controller.rb
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
post_image.rb
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
routes.rb
Rails.application.routes.draw do devise_for :users root 'post_images#index' resources :post_images, only: [:new, :create, :index, :show, :destroy] do resource :favorites, only: [:create, :destroy] resource :post_comments, only: [:create, :destroy] end resources :users, only: [:show, :edit, :update] # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
試したこと
link_toのパスをpost_images_path(投稿一覧ページ)に変えたところrouting_errorになった
補足情報(FW/ツールのバージョンなど)
Rails 5.2.4.1
ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-linux-gnu]
回答1件
あなたの回答
tips
プレビュー