I am trying to create a link_to to add(create instance) or delete(destroy instance) a deal to a wishlist, it looks like a heart and when clicked it create or delete a wishlist instance. It is working so far, but only problem is that it keeps refreshing the page, i've added remote: true but i keeps refreshing every time i add or remove from wishlist. I am using Rails 6
<%= link_to current_user.wishlist?(@deal) ? current_user.wishlist?(@deal) : deal_wishlists_path(@deal), class: "btn btn-light", method: current_user.wishlist?(@deal) ? :delete : :post, remote: true do %>
<% if current_user.wishlist?(@deal) %>
<i class="fas fa-heart"></i>
<span>Enregistré</span>
<% else %>
<i class="far fa-heart"></i>
<span>Wishlister</span>
<% end %>
<% end %>
class WishlistsController < ApplicationController
before_action :find_deal, except: :destroy
def create
@wishlist = @deal.wishlists.create!(user_id: current_user.id)
redirect_to deal_path(@deal)
end
def destroy
@wishlist = Wishlist.find(params[:id])
@wishlist.destroy
redirect_to deal_path(@wishlist.deal)
end
private
def find_deal
@deal = Deal.find(params[:deal_id])
end
end
thanks
Because you use redirect_to deal_path(...)
in your controller. Just remove them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.