0

I made a partial render "_likes.html.erb" as follow

<%= pluralize(post.get_likes.size, 'Like')%>
<% if current_user %>
  <% vote_text = current_user&.voted_for?(post) ? "unlike" : "Like" %>
  <%= button_to vote_text, like_post_path(post), method: :put, remote: :true, class:'like-post' %>
<% else %>
  <%= button_to "Like", new_user_session_path, class:'like-post' %>
<% end %>

and made a "like.js.erb" as follow

$("#like-count").html("<%= j render 'posts/like', post: @post %>")
$("#like-link").html("<%= j render 'posts/like_link', post: @post %>")

and def a method in Postscontroller as below

def like
    @post = Post.find(params[:id])
    if current_user.voted_for? @post
      @post.unliked_by current_user
    else
      @post.liked_by current_user
    end
  end

and then put that in the "show.html.erb" of "posts" app likes this

<%= render 'posts/likes', post: @post  %>

And I also remember to write acts_as_voter in user.rb and acts_as_votable in post.rb.

But I got nothing when i click the like button, the page freshed but the likes count stays alway 0

This is what I got in terminal after i click "like", can someone tell me what I did wrong, I'll be really thankful!!

tech-tonic-web-1  | Started GET "/posts/7?_method=put&authenticity_token=[FILTERED]" for 172.24.0.1 at 2023-05-03 07:53:26 +0000
tech-tonic-web-1  | Cannot render console from 172.24.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
tech-tonic-web-1  | Processing by PostsController#show as HTML
tech-tonic-web-1  |   Parameters: {"authenticity_token"=>"[FILTERED]", "id"=>"7"}
tech-tonic-web-1  |   User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
tech-tonic-web-1  |   Post Load (0.4ms)  SELECT "posts".* FROM "posts" WHERE "posts"."deleted_at" IS NULL AND "posts"."user_id" = $1 AND "posts"."id" = $2 LIMIT $3  [["user_id", 1], ["id", 7], ["LIMIT", 1]]
tech-tonic-web-1  |   ↳ app/controllers/posts_controller.rb:75:in `find_posts'
tech-tonic-web-1  |   Rendering layout layouts/application.html.erb
tech-tonic-web-1  |   Rendering posts/show.html.erb within layouts/application
tech-tonic-web-1  |   Tag Load (0.5ms)  SELECT "tags".* FROM "tags" INNER JOIN "post_tags" ON "tags"."id" = "post_tags"."tag_id" WHERE "post_tags"."post_id" = $1  [["post_id", 7]]
tech-tonic-web-1  |   ↳ app/views/posts/show.html.erb:29
tech-tonic-web-1  |   ActsAsVotable::Vote Count (0.6ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3  [["votable_id", 7], ["votable_type", "Post"], ["vote_flag", true]]
tech-tonic-web-1  |   ↳ app/views/posts/_likes.html.erb:1
tech-tonic-web-1  |   ActsAsVotable::Vote Exists? (0.5ms)  SELECT 1 AS one FROM "votes" WHERE "votes"."voter_id" = $1 AND "votes"."voter_type" = $2 AND "votes"."votable_id" = $3 AND "votes"."votable_type" = $4 AND "votes"."vote_scope" IS NULL LIMIT $5  [["voter_id", 1], ["voter_type", "User"], ["votable_id", 7], ["votable_type", "Post"], ["LIMIT", 1]]
tech-tonic-web-1  |   ↳ app/views/posts/_likes.html.erb:3
tech-tonic-web-1  |   Rendered posts/_likes.html.erb (Duration: 5.8ms | Allocations: 3089)
tech-tonic-web-1  |   Rendered comments/_form.html.erb (Duration: 1.3ms | Allocations: 648)
tech-tonic-web-1  |   Comment Load (0.4ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1  [["post_id", 7]]
tech-tonic-web-1  |   ↳ app/views/posts/show.html.erb:47
tech-tonic-web-1  |   Rendered comments/_comment.html.erb (Duration: 0.1ms | Allocations: 15)
tech-tonic-web-1  |   Rendered posts/show.html.erb within layouts/application (Duration: 14.0ms | Allocations: 6976)
tech-tonic-web-1  |   Rendered shared/_navbar.html.erb (Duration: 1.5ms | Allocations: 1035)
tech-tonic-web-1  |   Rendered layout layouts/application.html.erb (Duration: 28.7ms | Allocations: 19497)
tech-tonic-web-1  | Completed 200 OK in 39ms (Views: 28.6ms | ActiveRecord: 2.8ms | Allocations: 23252)
tech-tonic-web-1  |
tech-tonic-web-1  |

I was following the tutorial from YT and keep checking if I did anything wrong but still got no clue...

  • Looks like your JavaScript setup is not correct. See: https://guides.rubyonrails.org/working_with_javascript_in_rails.html#replacements-for-rails-ujs-functionality – spickermann May 03 '23 at 11:22
  • @spickermann Would you mind be more specific? I did't see anything wrong after checked out the doc. I will be really thankful if you could tell me which part the JS  is incorrect. – swaeetpotato May 04 '23 at 02:56

0 Answers0