【问题标题】:Rails: favorite a product and use ajax for the favorite/unfavorite buttonRails:收藏产品并使用 ajax 作为收藏/取消收藏按钮
【发布时间】:2019-03-04 09:28:59
【问题描述】:

我正在循环浏览一系列产品,并在每个产品旁边显示收藏/取消收藏按钮:

<% @products.each do |product| %>
<%= product.title %>
<%= product.price %>
<%= product.description %>

<div id="favorites_grid">
  <%= render partial: 'favorite_products/favorite', locals: { product: product } %>
</div>

带有收藏按钮的部分_favorite.html.erb:

<% unless current_user.favorite_products.exists?(product.id) %>
  <%= link_to "Favorite", create_favorited_product_path(product_id: product.id), method: :post, :remote => true %>
<% else %>
  <%= link_to "Unfavorite", favorited_product_path(product_id: product.id), method: :delete, :remote => true %>
<% end %>

表架构:

class CreateFavorites < ActiveRecord::Migration
  def change
    create_table :favorites do |t|
      t.references :favorited, polymorphic: true, index: true
      t.references :user, index: true

      t.timestamps
    end
  end
end

模型关联:

class Favorite < ApplicationRecord
  belongs_to :user
  belongs_to :favorited, polymorphic: true
end

class Consumer < ApplicationRecord
  has_many :favorites
  has_many :favorite_products, through: :favorites, source: :favorited, source_type: 'Product'
end

控制器:

def create
  @favorite = Favorite(favorited: @product, user_id: current_user.id)
    respond_to do |format|
      if @ favorite.save
        flash.now[:success] = "You have successfully added this product to your favorites list."
        format.js { render 'favorite.js.erb' }
      else
        flash.now[:alert] = "Something when wrong!"
        format.js { render 'favorite.js.erb' }
      end
    end
end

def destroy
  @favorite = Favorite.where(favorited_id: @product.id, user_id: current_user.id)
  respond_to do |format|
    @favorite.first.destroy
    flash.now[:success] = "You have successfully removed this product from your favorites list."
    format.js { render 'favorite.js.erb' }
  end
end

我禁用了涡轮链接。

最喜欢的.js.erb:

$('#favorites_grid').html("<%= escape_javascript(render partial: 'favorite_products/favorite', locals: { product: @product } ) %>");
$("#flash").html("<%= escape_javascript(render partial: 'layouts/messages') %>");

还有路线:

post 'favorite_products', to: 'favorite_products#create', defaults: { format: 'js' }, :as => 'create_favorited_product'
delete 'favorite_products/id', to: 'favorite_products#destroy', defaults: { format: 'js' }, :as => 'favorited_product'

收藏/取消收藏按钮在第一页加载时正确呈现在每个产品旁边。该按钮也可以正常工作,它收藏/取消收藏与其关联的产品。

我遇到的唯一问题是......如果我点击任何产品以收藏或取消收藏,ajax 会将第一个按钮呈现为收藏/取消收藏。换句话说,ajax 没有根据当前状态(喜欢或不喜欢)呈现正确的按钮。如果我刷新页面,一切都会正确显示。

关于如何解决此问题的任何想法?

【问题讨论】:

    标签: ruby-on-rails ajax


    【解决方案1】:

    所以您的问题是将&lt;div id="favorites_grid"&gt; 放入产品循环中,这会导致多个具有相同id 的div。然后,当您查找要替换的 div 时,您总是会得到第一个要替换的。

    我的建议是

    <% @products.each do |product| %>
      <%= product.title %>
      <%= product.price %>
      <%= product.description %>
    
      <div id="favorites_grid_<%= product.id %>">
        <%= render partial: 'favorite_products/favorite', locals: { product: product } %>
      </div>
    <% end %>
    

    然后您可以在favorite.js.erb 中查找依赖于favorites_grid_#{product.id} 的确切网格

    尝试更新:

    $("#favorites_grid_<%= @product.id %>").html("<%= escape_javascript(render partial: 'favorite_products/favorite', locals: { product: @product } ) %>");
    $("#flash").html("<%= escape_javascript(render partial: 'layouts/messages') %>");
    

    【讨论】:

    • 感谢@Hoa.Nguyen 的回复.. 不幸的是它不起作用,现在没有按钮更改。此外,我在 rails 控制台中也没有收到错误消息。你确定这是正确的&lt;div id="favorites_grid_#{product.id}"&gt; ??
    • 保持inspect窗口打开(我猜你用的是chrome),然后点击那个链接,你会看到一个JS响应,你能详细说明一下吗?
    • 我正在使用 safari...没有错误,浏览器控制台中没有显示任何内容!
    • 如果您使用的是 safari,则可以切换到网络选项卡,然后单击链接。在Name 列中将获取1个js文件到您的浏览器中,您可以点击它查看内容。
    • 第一个是这样的:curl 'https://localhost:3000/assets/jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1' \ -XGET这是正确的吗?
    猜你喜欢
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2023-03-15
    • 1970-01-01
    • 2017-10-30
    • 2019-08-12
    相关资源
    最近更新 更多