Rails, 销毁后渲染
Rails, rendering after destroy
我有一个 Cart/show.html erb,它呈现一个名为 carts_row.html.erb
的部分
<p id="notice"><%= notice %></p>
</br>
</br>
<div class="cart-container">
<%=render 'carts_row'%>
</div>
</p>
在 carts_row 中,它是一个购物车布局,其中包含用于更新购物车中的数量和 destroy/remove 商品的表单。
<h1>Shopping Cart</h1>
<div class="shopping-cart">
<div class="column-labels">
<label class="product-image">Image</label>
<label class="product-details">Product</label>
<label class="product-price">Price</label>
<label class="product-quantity">Quantity</label>
<!-- adding update column to labels -->
<label class="product-update">Update</label>
<label class="product-removal">Remove</label>
<label class="product-line-price">Total</label>
</div>
<%if @cart.cart_items.size == 0%>
<div class="text-center">
<p>Looks like you have nothing in your cart! <%=link_to 'Add something to your cart', products_path%></p>
</div>
<%end%>
<br />
<br />
<%if @cart_items != nil%>
<%@cart_items.each do |item|%>
<div class="product">
<div class="product-image">
<%=image_tag(item.product.picture)%>
</div>
<div class="product-details">
<div class="product-title"><%=item.product.name%></div>
<p class="product-description"><%=item.product.description%></p>
</div>
<div class="product-price"><%=number_to_currency(item.product.price)%></div>
<%=form_for item, remote: true do |f|%>
<div class="product-quantity">
<%=f.number_field :quantity, value: item.quantity, min: 1, max: 8%>
<%=f.hidden_field :product_id, value: item.product.id%>
<!-- <input type="number" value="<%=item.quantity%>" min="1" max="8"> -->
</div>
<div class="product-update">
<%=f.submit 'Update', class: "update-product"%>
<!-- <%=f.hidden_field :product_id, value: item.product.id%> -->
<!-- <button class="update-product">
Update
</button> -->
</div>
<div class="product-removal">
<%= link_to "Remove", item, {data: {confirm: "Are you sure you wish to delete the product '#{item.product.name}' from your cart?"}, method: :delete, remote: true, class: "remove-product"}%>
<!-- <button class="remove-product">
Remove
</button> -->
<%end%>
</div>
<div class="product-line-price"><%=number_to_currency(item.product.price*item.quantity)%></div>
</div>
<%end%>
<%end%>
<br />
<br />
<!--Temporary solution until model methods are written -->
<% if @cart_items != nil%>
<!--subtotal variable -->
<%subtotal =@cart_items.collect {|item| item.quantity*item.unit_price}.sum%>
<!--subtotal + shipping cost -->
<%total=subtotal+15%>
<%end%>
<div class="totals">
<div class="totals-item">
<label>Subtotal</label>
<div class="totals-value" id="cart-subtotal"><%=subtotal%></div>
</div>
<div class="totals-item">
<label>Tax (5%)</label>
<div class="totals-value" id="cart-tax">Included!</div>
</div>
<div class="totals-item">
<label>Shipping</label>
<div class="totals-value" id="cart-shipping">15.00</div>
</div>
<div class="totals-item totals-item-total">
<label>Grand Total</label>
<div class="totals-value" id="cart-total">
<%=total%>
</div></div>
</div>
</div>
<div class="checkout-btn">
<button class="checkout">Checkout</button>
</div>
</div>
表单中的更新转到 update.js.erb 其中有这个
<% if @cart.errors.any? || @cart_item.errors.any? %>
alert("Not valid.");
<%else%>
$(".cart-text").html("<%= escape_javascript(render 'layouts/cart_text')%>")
$(".cart-container").html("<%= escape_javascript(render 'carts/carts_row')%>")
<%end%>
和destroy的形式一样destroy.js.erb
<% if @cart.errors.any? || @cart_item.errors.any? %>
alert("Not valid.");
<%else%>
$(".cart-text").html("<%= escape_javascript(render 'layouts/cart_text')%>")
$(".cart-container").html("<%= escape_javascript(render 'carts/carts_row')%>")
<%end%>
更新正确呈现 carts_row.html.erb 表单。
当我销毁一个项目时,它会在页面底部呈现空产品和结账总额。当我刷新页面时,购物车中显示了正确数量的产品,一切都在它应该在的地方!
怎么update渲染正确,destroy渲染不正确???
我的 carts_item 控制器有这两种形式的方法:
def update
@cart = current_cart
# finding cart_items by cart_id
@cart_item = @cart.cart_items.find(params[:id])
# @cart_items.order(:id)
@cart_item.update_attributes(cart_item_params)
@cart_items = @cart.cart_items.order(:id)
# redirect 'cart_show_path'
end
def destroy
@cart = current_cart
@cart_item = @cart.cart_items.find(params[:id])
@cart_item.destroy
@cart_items = @cart.cart_items
end
您正在销毁购物车中的商品,而不是将其从 @cart.cart_items
中移除
尝试这样做
def destroy
@cart = current_cart
@cart_item = @cart.cart_items.find(params[:id])
#Find the index of @cart_item
item_index = @cart.cart_items.index(@cart_item)
#Remove it from @cart.cart_items when its destroyed successfully
if @cart_item.destroy
@cart.cart_items.delete_at(item_index)
end
@cart_items = @cart.cart_items
end
如果有效请告诉我!
我找到问题了。问题是我在我的视图中使用了 <% if @cart_items != nil %> 检查。
这总是 return 错误,因为 ActiveRecord 总是 return 一个数组!!!!!!!!!! @cart_items对象中是否有产品对象。
在我将此更改为使用 @cart_items.empty? 测试空数组后,页面就按我希望的方式工作了。
感谢 kumar 的意见。
我有一个 Cart/show.html erb,它呈现一个名为 carts_row.html.erb
的部分 <p id="notice"><%= notice %></p>
</br>
</br>
<div class="cart-container">
<%=render 'carts_row'%>
</div>
</p>
在 carts_row 中,它是一个购物车布局,其中包含用于更新购物车中的数量和 destroy/remove 商品的表单。
<h1>Shopping Cart</h1>
<div class="shopping-cart">
<div class="column-labels">
<label class="product-image">Image</label>
<label class="product-details">Product</label>
<label class="product-price">Price</label>
<label class="product-quantity">Quantity</label>
<!-- adding update column to labels -->
<label class="product-update">Update</label>
<label class="product-removal">Remove</label>
<label class="product-line-price">Total</label>
</div>
<%if @cart.cart_items.size == 0%>
<div class="text-center">
<p>Looks like you have nothing in your cart! <%=link_to 'Add something to your cart', products_path%></p>
</div>
<%end%>
<br />
<br />
<%if @cart_items != nil%>
<%@cart_items.each do |item|%>
<div class="product">
<div class="product-image">
<%=image_tag(item.product.picture)%>
</div>
<div class="product-details">
<div class="product-title"><%=item.product.name%></div>
<p class="product-description"><%=item.product.description%></p>
</div>
<div class="product-price"><%=number_to_currency(item.product.price)%></div>
<%=form_for item, remote: true do |f|%>
<div class="product-quantity">
<%=f.number_field :quantity, value: item.quantity, min: 1, max: 8%>
<%=f.hidden_field :product_id, value: item.product.id%>
<!-- <input type="number" value="<%=item.quantity%>" min="1" max="8"> -->
</div>
<div class="product-update">
<%=f.submit 'Update', class: "update-product"%>
<!-- <%=f.hidden_field :product_id, value: item.product.id%> -->
<!-- <button class="update-product">
Update
</button> -->
</div>
<div class="product-removal">
<%= link_to "Remove", item, {data: {confirm: "Are you sure you wish to delete the product '#{item.product.name}' from your cart?"}, method: :delete, remote: true, class: "remove-product"}%>
<!-- <button class="remove-product">
Remove
</button> -->
<%end%>
</div>
<div class="product-line-price"><%=number_to_currency(item.product.price*item.quantity)%></div>
</div>
<%end%>
<%end%>
<br />
<br />
<!--Temporary solution until model methods are written -->
<% if @cart_items != nil%>
<!--subtotal variable -->
<%subtotal =@cart_items.collect {|item| item.quantity*item.unit_price}.sum%>
<!--subtotal + shipping cost -->
<%total=subtotal+15%>
<%end%>
<div class="totals">
<div class="totals-item">
<label>Subtotal</label>
<div class="totals-value" id="cart-subtotal"><%=subtotal%></div>
</div>
<div class="totals-item">
<label>Tax (5%)</label>
<div class="totals-value" id="cart-tax">Included!</div>
</div>
<div class="totals-item">
<label>Shipping</label>
<div class="totals-value" id="cart-shipping">15.00</div>
</div>
<div class="totals-item totals-item-total">
<label>Grand Total</label>
<div class="totals-value" id="cart-total">
<%=total%>
</div></div>
</div>
</div>
<div class="checkout-btn">
<button class="checkout">Checkout</button>
</div>
</div>
表单中的更新转到 update.js.erb 其中有这个
<% if @cart.errors.any? || @cart_item.errors.any? %>
alert("Not valid.");
<%else%>
$(".cart-text").html("<%= escape_javascript(render 'layouts/cart_text')%>")
$(".cart-container").html("<%= escape_javascript(render 'carts/carts_row')%>")
<%end%>
和destroy的形式一样destroy.js.erb
<% if @cart.errors.any? || @cart_item.errors.any? %>
alert("Not valid.");
<%else%>
$(".cart-text").html("<%= escape_javascript(render 'layouts/cart_text')%>")
$(".cart-container").html("<%= escape_javascript(render 'carts/carts_row')%>")
<%end%>
更新正确呈现 carts_row.html.erb 表单。 当我销毁一个项目时,它会在页面底部呈现空产品和结账总额。当我刷新页面时,购物车中显示了正确数量的产品,一切都在它应该在的地方!
怎么update渲染正确,destroy渲染不正确???
我的 carts_item 控制器有这两种形式的方法:
def update
@cart = current_cart
# finding cart_items by cart_id
@cart_item = @cart.cart_items.find(params[:id])
# @cart_items.order(:id)
@cart_item.update_attributes(cart_item_params)
@cart_items = @cart.cart_items.order(:id)
# redirect 'cart_show_path'
end
def destroy
@cart = current_cart
@cart_item = @cart.cart_items.find(params[:id])
@cart_item.destroy
@cart_items = @cart.cart_items
end
您正在销毁购物车中的商品,而不是将其从 @cart.cart_items
尝试这样做
def destroy
@cart = current_cart
@cart_item = @cart.cart_items.find(params[:id])
#Find the index of @cart_item
item_index = @cart.cart_items.index(@cart_item)
#Remove it from @cart.cart_items when its destroyed successfully
if @cart_item.destroy
@cart.cart_items.delete_at(item_index)
end
@cart_items = @cart.cart_items
end
如果有效请告诉我!
我找到问题了。问题是我在我的视图中使用了 <% if @cart_items != nil %> 检查。
这总是 return 错误,因为 ActiveRecord 总是 return 一个数组!!!!!!!!!! @cart_items对象中是否有产品对象。
在我将此更改为使用 @cart_items.empty? 测试空数组后,页面就按我希望的方式工作了。
感谢 kumar 的意见。