Seems like the bookmark is being destroyed, but there's a problem causing an error 500 in the console and preventing the bookmark from being unselected. This issue arises when I update the page, indicating that it is indeed being destroyed, but for some reason, the AJAX request isn't completed. Here's the code snippet:
Bookmarks controller:
def destroy
@bookmark = Bookmark.find(params[:id])
@recommendation = Recommendation.find(@bookmark.recommendation_id)
if @bookmark.destroy
respond_to do |format|
format.html { redirect_to recommendation_path(@recommendation) }
format.js # <-- will render `app/views/reviews/create.js.erb`
end
else
flash[:notice] = "Couldn't delete bookmark"
redirect_back(fallback_location: root_path)
return
end
end
Routes:
resources :bookmarks, only: [:destroy]
get "/recommendations", to: "recommendations#index"
resources :recommendations, not: [:index] do
resources :reviews, only: [:new, :create, :destroy]
resources :bookmarks, only: [:create, :destroy]
end
Destroy.js.erb:
function refreshForm2() {
var els = document.querySelector("a[href='/bookmarks/<%<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dde09dbfb2b2b6b0bcafb6f3b4b9">[email protected]</a>%>']");
els.setAttribute("data-method", "post");
els.setAttribute("href", "/recommendations/<%= recommendation.id %>/bookmarks");
els.querySelector("i").setAttribute("class", "far fa-bookmark fa-xs")
}
refreshForm2();
The following error occurs:
application-852f66c6902b94f224b15c5a88c6c45599d9e733ce132ce9a28ec6a092031d74.js:216 DELETE http://localhost:3000/bookmarks/27 500 (Internal Server Error)
I'm at a loss here. What could be the mistake? The creation process works smoothly and updates the bookmark icon as expected.