Home>

I'm a beginner in programming and am currently trying to implement the likes of SNS-like applications.
I'm thinking of a completed model that changes color asynchronously when the button is pressed, and the number of likes fluctuates.
Currently, when I press the button, the database and terminal work, but there is no response on the browser,
For browser verification tools

rails-ujs.js: 216 POST http: // localhost: 3000/posts/1/likes /% 23% 3CLike :: ActiveRecord_Relation: 0x00007fd7a2d3eb00% 3E 500 (Internal Server Error)
./node_modules/@rails/ujs/lib/assets/compiled/rails-ujs.js.Rails.ajax @ rails-ujs.js: 216
./node_modules/@rails/ujs/lib/assets/compiled/rails-ujs.js.Rails.handleRemote @ rails-ujs.js: 652
(anonymous)
Network of verification tools
ActionView :: SyntaxErrorInTemplate in LikesController # create
Encountered a syntax error while rendering template: check $("# like-button"). html ("<% = j (render partial:'like', locals: {posts: @posts, likes: @likes, like: @ like post: @post})%>");


Error message is displayed.

I stopped without knowing how to use debugging and troubleshooting.

Corresponding source code
create.js.erb/destoroy.js.erb
$("# like-button"). html ("<% = j (render partial:'like', locals: {posts: @posts, likes: @likes, like: @like post: @post})%>");
_like.html.erb
<% if user_signed_in?%>
      <% if post.like_user (current_user.id)%>
        <% = button_to post_like_path (like, post_id: post.id), method:: delete, id: "like-button-# {post.id}", remote: true do%>
        <i></i>
          <span>
            <% = post.likes_count%>
          </span>
         <% end%>
      <% else%>
      <% = button_to post_likes_path (post), id: "like-button-# {post.id}", remote: true do%>
      <i></i>
        <span>
          <% = post.likes_count%>
        </span>
        <% end%>
      <% end%>
      <% else%>
        <i></i>
        <span>
          <% = post.likes_count%>
        </span>
<% end%>
likes_contoroller
class LikesController</pre>
<pre><code>index.html.erb

   Like<% = render partial:'likes/like', locals: {post: post, like: @likes}%>
like.rb
class Like
What I tried

https://qiita.com/YuitoSato/items/94913d6a349a530b2ea2

We are proceeding with the implementation with reference to this article.
By the time I got to status 500, I had to rewrite the code many times.
I investigated what the error of status 500 was, and learned that the cause could be found by looking at the network of verification tools, but I did not know what was wrong.

  • Answer # 1

    In the js file

    locals: {posts: @posts, likes: @likes, like: @like post: @post}


    Isn't it because the comma between @like and post is missing? ??