前提・実現したいこと
rails,jsでいいね機能を非同期で実装しようとしています。
詳細ページでは実装できるようになったのでトップページでも実装しようとした。その時のエラーを解決したいと思っています。何か分かりましたら回答よろしくお願いします。
<現在できている事>
商品詳細ページでは非同期でいいね機能を実装できています。
<前提>
詳細ページでは「お気に入り」という文字を表示されているがトップページでは表示させない。
<解決したい事・できるようにしたい事>
トップページからログインしているユーザーであれば非同期でいいね「登録」「解除」できるようにしたい。
発生している問題・エラーメッセージ
該当のソースコード
・ルーティング
routes
1中略 2 resources :products do 3 4 resources :favorites, only: [:create, :destroy] 5 end
・コントローラー
products
1def index 2 @products = Product.includes(:photos).order(created_at: :desc).limit(5) 3end 4 5def show 6 @products = Product.all 7 if @product == @products.first 8 @next_product = Product.next_search(@product) 9 elsif @product == @products.last 10 @prev_product = Product.prev_search(@product) 11 else 12 @prev_product = Product.prev_search(@product) 13 @next_product = Product.next_search(@product) 14 end 15 @grandchild = Category.find(@product.category_id) 16 @child = @grandchild.parent 17 @parent = @child.parent 18 @parent_category_products = @products.select { |product| product.category.root.name == @parent.name } 19 @user = current_user 20 Payjp.api_key = ENV['PAYJP_PRIVATE_KEY'] 21 @card = CreditCard.find_by(user_id: current_user) 22 if user_signed_in? 23 @favorite = Favorite.find_by(user_id: current_user.id, product_id: params[:id]) 24 end 25 end
favorites
1class FavoritesController < ApplicationController 2 3 def create 4 @favorite = Favorite.create(user_id: current_user.id, product_id: params[:product_id]) 5 @favorites = Favorite.where(product_id: params[:product_id]) 6 @product = Product.find(params[:product_id]) 7 end 8 9 def destroy 10 @favorite = Favorite.find_by(user_id: current_user.id, product_id: params[:product_id]) 11 @favorite.destroy 12 @favorites = Favorite.where(product_id: params[:product_id]) 13 @product = Product.find(params[:product_id]) 14 end 15end
・モデル
product
1中略 2 has_many :favorites, dependent: :destroy 3 has_many :favorite_users, through: :favorites, source: :user 4 5 def favorite_user(user_id) 6 favorites.find_by(user_id: user_id) 7 end
user
1中略 2 has_many :products, dependent: :destroy 3 has_many :favorites, dependent: :destroy 4 has_many :favorite_products, through: :favorites, source: :product 5 6 def already_favorited?(product) 7 self.favorites.exists?(product_id: product.id) 8 end
favorite
1class Favorite < ApplicationRecord 2 belongs_to :user 3 belongs_to :product 4 5 validates_uniqueness_of :product_id, scope: :user_id 6end
・ビュー
products/index.html.haml
index
1#products-favorites 2 = render "favorites/products_favorite", locals: {product: @product}
products/show.html.haml
show
1#favorites 2 = render "favorites/favorite", locals: {product: @product}
・部分テンプレート
favorites/_favorite.html.haml
- if user_signed_in? - if current_user.already_favorited?(product) = link_to product_favorite_path(product, @favorite), method: :delete, remote: true do %i.fas.fa-star お気に入り = product.favorites.count - else = link_to product_favorites_path(product), method: :post, remote: true do %i.far.fa-star お気に入り = product.favorites.count - else = icon('fa', 'star') お気に入り = product.favorites.count
favorites/_products_favorite.html.haml
- if user_signed_in? - if current_user.already_favorited?(@product) = link_to product_favorite_path(@product, @favorite), method: :delete, remote: true do %i.fas.fa-star = @product.favorites.count - else = link_to product_favorites_path(@product), method: :post, remote: true do %i.far.fa-star = @product.favorites.count - else = icon('fa', 'star') = @product.favorites.count
・jsファイル
favorites/create.js.haml、destroy.js.haml
$("#favorites").html("#{j(render partial: "favorites/favorite", locals: { product: @product })}"); $("#products-favorites").html("#{j(render partial: "favorites/products_favorite", locals: { product: @product })}");
試したこと
@product.present?を試すと返り値としてfalseが、@products.present?を試すと返り値としてtrueが帰ってきた。部分テンプレートの@productを@productsに変更したが別のエラーが出た。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/16 13:22
2020/08/16 13:28
2020/08/16 13:37
2020/08/16 13:54
2020/08/16 14:26
2020/08/16 22:11
2020/08/17 09:31
2020/08/17 09:52