我们需要在进行 AJAX 调用时传递图像 id,以便在 AJAX 完成时渲染局部视图时可以在视图中获取 @image 对象。
因此我们使用 image_id 参数获取@image。
show.html.erb
<div id="cart_form">
<% if @cart_item_collection.include?(@image.id) %>
<%= render 'shared/forms/remove_from_cart', locals: { image: @image } %>
<% else %>
<%= render 'shared/forms/add_to_cart', locals: { image: @image } %>
<% end %>
<p class="error"></p>
</div>
images_controller.rb
class ImagesController < ApplicationController
def show
@image = Image.find(params[:id])
@cart_item_collection = CartItem.where(user_id: current_or_guest_user).pluck(:image_id)
end
end
_add_to_cart.html.erb
<%= form_for(@cart_item, url: cart_item_path, :remote => true do |f| %>
<%= f.hidden_field :image_id, value: @image.id %>
<%= f.submit "Add To Cart" %>
<% end %>
_remove_from_cart.html.erb
<%=button_to cart_item_path(id: @cart_item, image_id: @image.id), method: :delete, :remote => true, class: "icon di_darckblue extra-large button fill", name: 'images' do %>
<i class="di-shopping-cart-up"></i>
Remove From Cart
加入购物车,
def add_to_cart
@image = Image.find(params[:cart_item][:image_id])
#if @image is present
#your code
#else
#@error= "Item could not be added to cart."
#end
end
create.js.erb
<% if !@error %>
$("#cart_form").html("<%= j render partial: 'shared/forms/remove_from_cart', locals: { image: @image } %>");
$('.error').html('');
<% else %>
$('.error').html(<%= @error %>);
<% end %>
从购物车中删除,
def destroy
@image = Image.find(params[:image_id])
#if @image
#destroying @cart_item
#rendering destroy.js.erb
#@cart_item = CartItem.new
#else
@error="Item could not be removed from cart"
#end
end
destroy.js.erb:
<% if !@error %>
$("#cart_form").html('<%= j render partial: "shared/forms/add_to_cart", locals: { image: @image } %>');
$('.error').html('');
<% else %>
$('.error').html(<%= @error %>);
<% end %>
另一种方式:
因为您只是在部分中使用图像 id:
在这里,我们只是在控制器中传递 image_id 并将其发送回前端。
但我们唯一要做的检查是检查传递给控制器的 image_id 的图像是否存在,因为用户有可能在前端更改值。
为了检查我们必须在每次 AJAX 调用时检查图像的存在,这使得第一种方法更安全。但是,如果这对您来说并不重要,那么您可以采用这种方法。
<div id="cart_form">
<% if @cart_item_collection.include?(@image.id) %>
<%= render 'shared/forms/remove_from_cart', locals: { image_id: @image.id } %>
<% else %>
<%= render 'shared/forms/add_to_cart', locals: { image_id: @image.id } %>
<% end %>
</div>
class ImagesController < ApplicationController
def show
@image = Image.find(params[:id])
@cart_item_collection = CartItem.where(user_id: current_or_guest_user).pluck(:image_id)
end
end
_add_to_cart.html.erb
<%= form_for(@cart_item, url: cart_item_path, :remote => true do |f| %>
<%= f.hidden_field :image_id, value: image_id %>
<%= f.submit "Add To Cart" %>
<% end %>
_remove_from_cart.html.erb
<%=button_to cart_item_path(id: @cart_item, image_id: image_id), method: :delete, :remote => true, class: "icon di_darckblue extra-large button fill", name: 'images' do %>
<i class="di-shopping-cart-up"></i>
Remove From Cart
添加到购物车,我渲染这个 js.erb 文件,
def add_to_cart
@image_id = params[:cart_item][:image_id]
#your code
end
create.js.erb
$("#cart_form").html("<%= j render partial: 'shared/forms/remove_from_cart', locals: { image_id: @image_id } %>");
从购物车中删除,
def destroy
@image = params[:image_id]
#destroying @cart_item
#rendering destroy.js.erb
end
destroy.js.erb:
$("#cart_form").html('<%= j render partial: "shared/forms/add_to_cart", locals: { image_id: @image_id } %>');