●実現したいこと
現在、ユーザーの作品を投稿できるInstagramのようなサイトを作成しています。
その中で、作品に「いいね」が押せるように機能を追加しようとしているのですが、「いいねボタン」を押しても、リロードしないと結果が反映されない状態が出ています。
●質問
以下エラー文にあるart.like_userのartがnilになっていることは、何となく感じているのですが、その対処法がわからず悩んでおります。
●「いいねボタン」を押した際に起きているエラー
ActionView::Template::Error (undefined local variable or method `art' for #<#<Class:0x007ff54d1973b0>:0x007ff54b9f6f08> Did you mean? arts art_url @arts): 1: <% if user_signed_in? %> 2: <% if art.like_user(current_user.id) %> 3: <%= button_to art_likes_path(like, art_id: art.id), method: :delete, id: "like-button", remote: true do %> 4: <%= image_tag("icon_red_heart.svg") %> 5: <span> app/views/likes/_like.html.erb:2:in `_app_views_likes__like_html_erb___1033727092812393927_70345769537660' app/views/likes/create.js.erb:1:in `_app_views_likes_create_js_erb__3536096750636237475_70345756223260'
●各ファイルの状態 index.html.erb
<% @arts.each do |art| %> <div class="contents col-md-3 clearfix"> <div class="art_image"> <%= image_tag art.image.url %> </div> <span class="user_data">投稿者:</span><%= art.user.nickname %> <% if user_signed_in? && current_user.id == art.user_id %> <ul class="more_list"> <li> <%= link_to '編集', "/arts/#{art.id}/edit", method: :get %> </li> <li> <%= link_to '削除', "/arts/#{art.id}", method: :delete %> </li> </ul> <% end %> <div class="like"> <%= render partial: 'likes/like', locals: { art: art, arts: @arts, likes: @likes, like: @like } %> </div> </div> <% end %>
●各ファイルの状態 _like.html.erb
<% if user_signed_in? %> <% if art.like_user(current_user.id) %> <%= button_to art_likes_path(like, art_id: art.id), method: :delete, id: "like-button", remote: true do %> <%= image_tag("icon_red_heart.svg") %> <span> <%= art.likes_count %> </span> <% end %> <% else %> <%= button_to art_likes_path(art), id: "like-button", remote: true do %> <%= image_tag("icon_heart.svg") %> <span> <%= art.likes_count %> </span> <% end %> <% end %> <% else %> <%= image_tag("icon_heart.svg") %> <span> <%= art.likes_count %> </span> <% end %>
●各ファイルの状態 routes.rb
Rails.application.routes.draw do devise_for :users resources :users, only: [:show, :edit, :update] root to: "arts#index" resources :arts, only: [:index, :new, :create, :show, :edit, :update, :destroy] do resources :likes, only: [:create, :destroy] end end
●各ファイルの状態 likes_controller
class LikesController < ApplicationController def create @like = Like.create(user_id: current_user.id, art_id: params[:art_id]) @likes = Like.where(art_id: params[:art_id]) @arts = Art.all end def destroy like = Like.find_by(user_id: current_user.id, art_id: params[:art_id]) like.destroy @likes = Like.where(art_id: params[:art_id]) @arts = Art.all end end
●各ファイルの状態 create.js.erb
$("#like-button").html("<%= j(render partial: 'likes/like', locals: { arts: @arts, likes: @likes, like: @like}) %>");
●各ファイルの状態 destroy.js.erb
$("#like-button").html("<%= j(render partial: 'likes/like', locals: { arts: @arts, likes: @likes }) %>");
●各ファイルの状態 art.rb
class Art < ActiveRecord::Base belongs_to :user mount_uploader :image, ImageUploader has_many :likes, dependent: :destroy def like_user(user_id) likes.find_by(user_id: user_id) end end
各ファイルの状態 like.rb
class Like < ActiveRecord::Base belongs_to :art, counter_cache: :likes_count belongs_to :user end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/28 14:11