【问题标题】:link_to with remote: true still refreshing the pagelink_to with remote: true 仍在刷新页面
【发布时间】:2021-07-20 18:39:27
【问题描述】:

我正在尝试创建一个 link_to 以向愿望清单添加(创建实例)或删除(销毁实例)交易,它看起来像一颗心,单击它时会创建或删除愿望清单实例。到目前为止它正在工作,但唯一的问题是它不断刷新页面,我添加了 remote: true 但每次我从愿望清单中添加或删除时我都会不断刷新。我正在使用 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

谢谢

【问题讨论】:

  • 这不是按钮,而是您的控制器。它每次都会重定向。要在不重新加载页面的情况下更新页面,您需要以不同的方式处理 ajaxy 请求。看看这个帖子,例如:medium.com/swlh/…应该给你一个想法。

标签: jquery ruby-on-rails


【解决方案1】:

因为您在控制器中使用了 redirect_to deal_path(...)。只需删除它们。

【讨论】:

    猜你喜欢
    • 2016-04-28
    • 2018-02-26
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 2011-08-20
    相关资源
    最近更新 更多