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.
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_path
Although 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