[RUBY] Questions about implementing the Like feature (Ajax) in Rails

Overview

Ajax processing doesn't work well when creating a portfolio and implementing likes using Ajax This article https://qiita.com/naota7118/items/e009eff939b5a764672d#%E4%BB%8A%E5%9B%9E%E8%8B%A6%E5%8A%B4%E3%81%97%E3%81%9F%E3%81%A8%E3%81%93%E3%82%8D I read and solved it. But, As you can see in this article

<%= button_to question_like_path(question, id: question.likes[0].id), class:"btn btn-primary like", method: :delete , remote:true do %>

I didn't understand why this code works with the destroy action that removes my likes (id: question.likes [0] .id erases the 0th like of likes linked to the question without asking questions, so I thought I couldn't guarantee to erase my likes? T)

I twisted my head and understood it, so I will leave it as a memorandum.

Conclusion

Because the like to be deleted on the destroy action side is find_by with current_user

  def destroy
    @like = @question.likes.find_by(user_id: current_user.id)
    @like.destroy
    #Root for the time being until Ajax is implemented later
    respond_to do |format|
      format.html { redirect_to @like.question || root_url}
      format.js
    end
  end

question_like_pathAlthough the question and like to be deleted are required as arguments Since URL cannot be generated unless like is specified, likes [0] is used instead. However, the like user to be erased uses the `` `current_user``` method. It doesn't matter here because it is obtained by find_by in the destroy action.

I was deceived by the appearance. .. ..

Recommended Posts

Questions about implementing the Like feature (Ajax) in Rails
About the symbol <%%> in Rails erb
How to implement a like feature in Rails
about the where method (rails)
I tried to implement Ajax processing of like function in Rails
[Rails] About the Punk List function
[Rails] About implementation of like function
First pagination feature added in rails
I summarized the flow until implementing simple_calendar in Ruby on Rails.
[Rails] About the error that the image is not displayed in the production environment
The identity of params [: id] in rails
Rails refactoring story learned in the field
Implemented follow function in Rails (Ajax communication)
[Rails] About local: true described in form_with
[Rails] Reset the database in the production environment
Rails6: Extract the image in Action Text
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
About Rails 6
About the case where "Docker" freeter tried to put Docker in the existing Rails application
[Order method] Set the order of data in Rails
About the confusion seen in startup Java servers
Think about the combination of Servlet and Ajax
[Rails / ActiveRecord] About the difference between create and create!
About the idea of anonymous classes in Java
A story about the JDK in the Java 11 era
I tried to organize the session in Rails
rails tutorial About account activation in production environment
[Rails] Show multi-level categories in the breadcrumb trail
A note about the Rails and Vue process
How to check Rails commands in the terminal