実現したいこと
非同期のいいね機能を実装時、2つある非同期いいねのうち1つだけ非同期でいいねがされない為、2つとも非同期いいねをさせたい。
発生している問題・エラーメッセージ
非同期いいねされないボタンを押すと下記のエラーメッセージが表示されます。
No template found for CosplayFavoritesController#favorite, rendering head :no_content
該当のソースコード
cosplay_favorite.rb
class CosplayFavorite < ApplicationRecord belongs_to :post_image, counter_cache: :cosplay_favorites_count belongs_to :user end
post_image.rb
class PostImage < ApplicationRecord belongs_to :user has_many :favorites, dependent: :destroy has_many :fav_users, through: :favorites, source: :user has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_users, through: :cosplay_favorites, source: :user has_many :post_comments, dependent: :destroy attachment :real_image attachment :cosplay_image default_scope -> { order(created_at: :asc) } end
user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :post_images, dependent: :destroy has_many :favorites, dependent: :destroy has_many :fav_post_images, through: :favorites, source: :post_image has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_post_images, through: :favorites, source: :post_image has_many :post_comments, dependent: :destroy validates :name, presence: true, length: { in: 2..15 } attachment :profile_image end
cosplay_favorites_controller.rb
class CosplayFavoritesController < ApplicationController before_action :set_variables def favorite favorite = current_user.cosplay_favorites.new(post_image_id: @post_image.id) favorite.save end def unfavorite favorite = current_user.favorites.find_by(post_image_id: @post_image.id) favorite.destroy end private def set_variables @post_image = PostImage.find(params[:id]) @id_name = "#cosplay_favorite-link-#{@post_image.id}" end end
post_images/_cosplay_post_images.html.erb
<%= render 'cosplay_favorites/cosplay_favorite', post_image: post_image %>
cosplay_favorites/_cosplay_favorite.html.erb
<div class="cosplay_favorite-link" id="cosplay_favorite-link-<%= post_image.id %>"> <% if current_user.cosplay_favorites.find_by(post_image_id: post_image.id) %> <%= link_to cosplay_favorite_path(post_image.id), method: :post, remote: true do %> <div class = "iine__button">❤️<%= post_image.cosplay_favorites.count %> コスプレの写真いいね!</div> <% end %> <% else %> <%= link_to cosplay_favorite_path(post_image.id), method: :post, remote: true do %> <div class = "iine__button">♡️<%= post_image.cosplay_favorites.count %> コスプレの写真いいね!</div> <% end %> <% end %> </div>
cosplay_favorites/cosplay_favorite.js.erb
$("<%= @id_name %>").html('<%= escape_javascript(render("cosplay_favorites/cosplay_favorite", post_image: @post_image )) %>');
post_images/index.html.erb
<div class="header"> <nav class="navigation"> <img src="/assets/logo.png"> <ul> <li> <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> </li> <li> <%= link_to '投稿する', new_post_image_path %> </li> <li> <%= link_to 'マイページ', user_path(current_user.id) %> </li> </ul> </nav> </div> <div class="post_images_index_wrapper"> <% @post_images.each do |post_image| %> <div class="index_box"> <div class="post_images_index_user"> <ul> <li><%= attachment_image_tag @user, :profile_image, fallback: "no_image.jpg" %></li> <li><p><%= post_image.user.name %></p></li> </ul> </div> <div class="post_images_index_title"> <div class="image_title"> <h2>Real</h2> </div> <div class="image_title"> <h2>Cosplay</h2> </div> </div> <div class="post_images_box"> <div class="post_image"> <%= attachment_image_tag post_image, :real_image %> </div> <div class="post_image"> <%= attachment_image_tag post_image, :cosplay_image %> </div> </div> <div class="image_name"> <%= post_image.real_image_name %> </div> <div class="image_name"> <%= post_image.cosplay_image_name %> </div> <div class="favorite_area"> <%= render partial: 'post_images/post_images', locals: { post_image: post_image } %> </div> <div class="favorite_area"> <%= render partial: 'post_images/cosplay_post_images', locals: { post_image: post_image } %> </div> <div class="image_caption"> <ul> <li> <%= attachment_image_tag @user, :profile_image, fallback: "no_image.jpg" %> </li> <li> <p><%= post_image.user.name %> </p> </li> <li> <p><%= link_to "#{post_image.post_comments.count}件のコメント", post_image_path(post_image.id) %></p> </li> </ul> <span class="caption"><%= post_image.caption %></span> </div> <% if post_image.user == current_user %> <%= link_to "削除", post_image_path(post_image), method: :delete %> <% end %> </div> <% end %> <%= paginate @post_images, class: "pagenate" %> </div>
試したこと
色々調べると、CosplayFavoritesController#favoriteに対応するテンプレートがないというエラーメッセージということが分かり、viewを作成しましたが解決しませんでした。
補足情報(FW/ツールのバージョンなど)
ruby 2.5.7p206
Rails 5.2.4.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。