###前提・実現したいこと
現在、Ajax無しではredirect_toで処理でいいね!処理をしている状態です。そこでAjaxと取り入れ、ページの更新無しに部分更新をしたく存じます。
https://qiita.com/naberina/items/c6b5c8d7756cb882fb20を参考に作っています。
しかし、いいね!ボタンを押すと、反応はなく、ページ更新すると反映されます。
ログを確認すると、jsファイルへpostの値をうまく渡せずにエラーを起こしています。
###エラー
Error
1*いいね!create時 2Started POST "/likes/3/create" for 127.0.0.1 at 2019-06-14 14:53:51 +0900 3Processing by LikesController#create as JS 4 Parameters: {"post_id"=>"3"} 5 User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 6 ↳ app/controllers/application_controller.rb:6 7 Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" IS NULL LIMIT ? [["LIMIT", 1]] 8 ↳ app/controllers/likes_controller.rb:7 9 (0.1ms) begin transaction 10 ↳ app/controllers/likes_controller.rb:9 11 CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 12 ↳ app/controllers/likes_controller.rb:9 13 Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]] 14 ↳ app/controllers/likes_controller.rb:9 15 Like Create (0.5ms) INSERT INTO "likes" ("user_id", "post_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["user_id", 1], ["post_id", 3], ["created_at", "2019-06-14 05:53:51.570765"], ["updated_at", "2019-06-14 05:53:51.570765"]] 16 ↳ app/controllers/likes_controller.rb:9 17 (1.0ms) commit transaction 18 ↳ app/controllers/likes_controller.rb:9 19 Rendering likes/create.js.erb 20 Rendered likes/create.js.erb (18.0ms) 21Completed 500 Internal Server Error in 112ms (ActiveRecord: 2.2ms) 22 23 24 25ActionView::Template::Error (undefined method `id' for nil:NilClass): 26 1: $('#likes_buttons_<%= @post.id %>').html("<%= j(render partial: 'likes/like', locals: {post: @post}) %>"); 27 28app/views/likes/create.js.erb:1:in `_app_views_likes_create_js_erb___1624215930080801267_70224208720440' 29 30*いいね!destroy時 31Started POST "/likes/3/destroy" for 127.0.0.1 at 2019-06-14 14:46:30 +0900 32Processing by LikesController#destroy as JS 33 Parameters: {"post_id"=>"3"} 34 User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 35 ↳ app/controllers/application_controller.rb:6 36 Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" IS NULL LIMIT ? [["LIMIT", 1]] 37 ↳ app/controllers/likes_controller.rb:15 38 Like Load (0.2ms) SELECT "likes".* FROM "likes" WHERE "likes"."user_id" = ? AND "likes"."post_id" = ? LIMIT ? [["user_id", 1], ["post_id", 3], ["LIMIT", 1]] 39 ↳ app/controllers/likes_controller.rb:16 40 (0.1ms) begin transaction 41 ↳ app/controllers/likes_controller.rb:17 42 Like Destroy (0.4ms) DELETE FROM "likes" WHERE "likes"."id" = ? [["id", 27]] 43 ↳ app/controllers/likes_controller.rb:17 44 (1.0ms) commit transaction 45 ↳ app/controllers/likes_controller.rb:17 46 Rendering likes/destroy.js.erb 47 Rendered likes/destroy.js.erb (22.8ms) 48Completed 500 Internal Server Error in 116ms (ActiveRecord: 2.0ms) 49 50 51 52ActionView::Template::Error (undefined method `id' for nil:NilClass): 53 1: $('#likes_buttons_<%= @post.id %>').html("<%= j(render partial: 'likes/like', locals: {post: @post}) %>"); 54 55app/views/likes/destroy.js.erb:1:in `_app_views_likes_destroy_js_erb__3123402465132948964_70224203960620'
ちなみに、@post→postに変更した場合、ActionViewはこのような記載になります。
Error
1*いいね!create時 2ActionView::Template::Error (undefined local variable or method `post' for #<#<Class:0x00007fbcb1769a60>:0x00007fbcad37bd30> 3Did you mean? @post 4 @posts): 5 1: $('#likes_buttons_<%= post.id %>').html("<%= j(render partial: 'likes/like', locals: {post: post}) %>"); 6*いいね!destroy時 7ActionView::Template::Error (undefined local variable or method `post' for #<#<Class:0x00007fbcb1769a60>:0x00007fbcb16a9cd8> 8Did you mean? @post 9 @posts): 10 1: $('#likes_buttons_<%= post.id %>').html("<%= j(render partial: 'likes/like', locals: {post: post}) %>");
###コード
index
1<% @posts.each do |post| %> 2<div id='likes_buttons_<%= post.id %>'> 3 <%= render partial: 'likes/like', locals: { post: post} %> 4 </div> 5<% end %>
*_like.html.erb <% if Like.find_by(user_id: @current_user.id, post_id: post.id) %> <%= link_to("/likes/#{post.id}/destroy", method: :post, remote: true) do %> <span class="fa fa-heart icon-size like-btn-unlike"></span> <% end %> <% else %> <%= link_to("/likes/#{post.id}/create", method: :post, remote: true) do %> <span class="fa fa-heart icon-size like-btn"></span> <% end %> <% end %> <%= Like.where(post_id: post).count %>
js
1*create.js.erb 2$('#likes_buttons_<%= @post.id %>').html("<%= j(render partial: 'likes/like', locals: {post: @post}) %>");
js
1*create.js.erb 2$('#likes_buttons_<%= @post.id %>').html("<%= j(render partial: 'likes/like', locals: {post: @post}) %>");
js
1*application.js 2//= require rails-ujs 3//= require jquery 4//= require jquery_ujs 5//= require activestorage 6//= require turbolinks 7//= require_tree .
Controller
1class LikesController < ApplicationController 2 before_action :authenticate_user 3 4 def create 5 @posts = Post.all.order(created_at: :desc) 6 @post = Post.find_by(id: params[:id]) 7 @like = Like.new(user_id: @current_user.id, post_id: params[:post_id]) 8 @like.save 9 end 10 def destroy 11 @posts = Post.all.order(created_at: :desc) 12 @post = Post.find_by(id: params[:id]) 13 @like = Like.find_by(user_id: @current_user.id, post_id: params[:post_id]) 14 @like.destroy 15 end 16end
回答1件
あなたの回答
tips
プレビュー