1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

いいねボタンのAjaxが機能しません。リロードをしなければ反応しません。

Last updated at Posted at 2022-03-27

エラー

いいねボタンを押しても反応しません。

destroy

エラーメッセージがなかったのでログを載せました。

Started DELETE "/favorites/4950" for 14.12.68.160 at 2022-03-27 02:18:28 +0000
Cannot render console from 14.12.68.160! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by FavoritesController#destroy as JS
  Parameters: {"authenticity_token"=>"*******==", "id"=>"4950"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/helpers/sessions_helper.rb:17:in `current_user'
  Micropost Load (0.1ms)  SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" = ? ORDER BY "microposts"."created_at" DESC LIMIT ?  [["id", 4950], ["LIMIT", 1]]
  ↳ app/controllers/favorites_controller.rb:14:in `destroy'
  Favorite Load (0.1ms)  SELECT "favorites".* FROM "favorites" WHERE "favorites"."user_id" = ? AND "favorites"."micropost_id" = ? ORDER BY "favorites"."created_at" DESC LIMIT ?  [["user_id", 1], ["micropost_id", 4950], ["LIMIT", 1]]
  ↳ app/models/user.rb:113:in `unlike'
  TRANSACTION (0.1ms)  begin transaction
  ↳ app/models/user.rb:113:in `unlike'
  Favorite Destroy (2.5ms)  DELETE FROM "favorites" WHERE "favorites"."id" = ?  [["id", 1257]]
  ↳ app/models/user.rb:113:in `unlike'
  TRANSACTION (6.4ms)  commit transaction
  ↳ app/models/user.rb:113:in `unlike'
  Rendering favorites/destroy.js.erb
   (0.2ms)  SELECT COUNT(*) FROM "users" INNER JOIN "favorites" ON "users"."id" = "favorites"."user_id" WHERE "favorites"."micropost_id" = ?  [["micropost_id", 4950]]
  ↳ app/views/favorites/_like.html.erb:3
  Rendered favorites/_like.html.erb (Duration: 3.6ms | Allocations: 1098)
  Rendered favorites/destroy.js.erb (Duration: 4.7ms | Allocations: 1402)
Completed 200 OK in 31ms (Views: 7.4ms | ActiveRecord: 9.4ms | Allocations: 7050)

create

ActionController::UnknownFormat (ActionController::UnknownFormat):
  
app/controllers/favorites_controller.rb:7:in `create'

いいねボタン

app/views/microposts/_micropost.html.erb

<li id="micropost-<%= micropost.id %>">
  .
  .
  .
  <span class="timestamp">>
    <%= render 'favorites/like_form', micropost: micropost %>
.
.
.
</li>

app/views/favorites/_like_form.html.erb

<div id="like_form">
<% if current_user.like?(micropost) %>
  <%= render 'favorites/unlike', micropost: micropost %>
<% else %>
  <%= render 'favorites/like', micropost: micropost %>
<% end %>
</div>

favorites/_like.html.erb

<%= button_to favorites_path(micropost), method: :post, remote: true do %>
  <%= debug favorites_path(micropost) %>
  <%= micropost.liked.count %>
  <span >♡</span>
<% end %>

favorites/_unlike.html.erb

<%= button_to favorite_path(micropost), method: :delete, remote: true do %>
  <%= debug favorite_path(micropost) %>
  <%= micropost.liked.count %>
  <span style = "color:red;"></span>
<% end %>

コントローラー

app/controllers/favorites_controller.rb

class FavoritesController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]
 
  def create
    @micropost = Micropost.find_by(id: params[:format])
    current_user.like(@micropost)
    respond_to do |format|
      format.html { redirect_to request.referer }
      format.js  
    end
  end
  
  def destroy
    @micropost = Micropost.find_by(id: params[:id])
    current_user.unlike(@micropost)
    respond_to do |format|
      format.html { redirect_to request.referer }
      format.js
    end
  end
end

app/views/favorites/create.js.erb

$("#like_form").html("<%= escape_javascript(render(partial: 'favorites/unlike', locals: { micropost: @micropost })) %>");

app/views/favorites/destroy.js.erb

$("#like_form").html("<%= escape_javascript(render(partial: 'favorites/like', locals: { micropost: @micropost })) %>");

モデル

app/models/user.rb

.
.
.
 # いいねする
  def like(other_micropost)
    likes << other_micropost
  end

  # いいね解除
  def unlike(other_micropost)
    favorites.find_by(micropost_id: other_micropost.id).destroy
  end

  # いいねしているか?
  def like?(other_micropost)
    likes.include?(other_micropost)
  end
.
.
.
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?