ご覧いただきありがとうございます。
現在教材をもとにインスタのようなrailsアプリを作成中です。
いいね機能をつけているのですが
いいねを押した後、いいねを取り消すという文字には切り替わるのですが
その後いいねを取り消すを押しても反応しません。
自分なりに調べましたが原因がわからず、どなたかご教授いただけましたら助かります。
ログは下記の通りです。
Started DELETE "/posts/1/likes/1" for 172.18.0.1 at 2021-10-17 08:46:58 +0000 Completed 500 Internal Server Error in 101ms (ActiveRecord: 2.5ms) Completed 500 Internal Server Error in 177ms (ActiveRecord: 4.9ms) Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by LikesController#destroy as JS Rendering posts/show.html.erb within layouts/application NoMethodError (undefined method `post' for nil:NilClass): NoMethodError (undefined method `post' for nil:NilClass): Like Load (1.3ms) SELECT `likes`.* FROM `likes` WHERE `likes`.`user_id` = 1 AND `likes`.`post_id` = 1 LIMIT 1 Parameters: {"post_id"=>"1", "id"=>"1"} User Load (5.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 Rendered posts/show.html.erb within layouts/application (22.7ms) app/controllers/likes_controller.rb:14:in `destroy' app/controllers/likes_controller.rb:14:in `destroy' Like Load (1.7ms) SELECT `likes`.* FROM `likes` WHERE `likes`.`id` = 1 LIMIT 1 Completed 500 Internal Server Error in 254ms (ActiveRecord: 6.9ms) NoMethodError (undefined method `post' for nil:NilClass): app/controllers/likes_controller.rb:14:in `destroy' Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (2.1ms) Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.1ms) Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.0ms) Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (358.9ms) Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (1.0ms) Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (6.6ms) Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.8ms) Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.7ms) Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.3ms) Rendering /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (305.1ms) Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (2.2ms) Rendered /usr/local/bundle/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (261.0ms) Completed 200 OK in 2667ms (Views: 2524.5ms | ActiveRecord: 4.6ms)
となっていましたのでdestroyを確認しました。
def destroy @like = Like.find_by(id: params[:id]) @post = @like.post if @like.destroy respond_to :js end end
特に打ち間違いなどもありませんでした。
またNoMethodError (undefined method post' for nil:NilClass): というところから
エラーを探そうとコンソールで調べましたが下記の通りに表示されました。
@like = Like.find_by(id: params[:id]) NameError: undefined local variable or methodparams' for main:Object
調べましたがこれはhttpリクエストが発行されてからparamsが発生するためこのエラーは問題ないように思います。
destroy周辺でエラーになっているとは思うのですが
これ以降どのようにしたらいいかわからず質問させていただきました。
※リロードするといいねに切り替わります。
お手数ですが教えていただけましたら助かります。
何卒宜しくお願い申し上げます。
show.html.erb
<div id="like-icon-post-<%= @post.id.to_s %>"> <% if @post.liked_by(current_user).present? %> <%= link_to "いいねを取り消す",post_like_path(@post.id,@post.liked_by(current_user)),method: :DELETE, remote: true, class: "loved hide-text"%> <% else %> <%= link_to "いいね",post_likes_path(@post),method: :POST,remote: true, class: "love hide-text"%> <%end%> </div>
destroy.js.erb
$('#like-icon-post-<%= @post.id.to_s%>'). html('<%= link_to "いいね",post_likes_path(@post),method: :POST,remote: true,class: "love hide-text" %>');
create.js.erb
$('#like-icon-post-<%= @post.id.to_s %>'). html('<%= link_to "いいねを取り消す", post_like_path(@post.id, @like), method: :DELETE, remote: true, class: "loved hide-text" %>');
LikesController
def create @like = current_user.likes.build(like_params) @post = @like.post if @like.save respond_to :js end end
routes.rb
Rails.application.routes.draw do root to: 'home#top' devise_for :users resources :users resources :posts do resources :likes, only: [:create, :destroy] resources :comments end end
post.rb
class Post < ApplicationRecord belongs_to :user has_many :likes,dependent: :destroy def liked_by(user) Like.find_by(user_id: user.id, post_id: id) end end
like.rb
class Like < ApplicationRecord belongs_to :user belongs_to :post validates :user_id, uniqueness: { scope: :post_id } end
環境は以下の通りです。
macOS Catalina バージョン 10.15.6
Ruby 2.6.6
Rails 5.2.6
宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー