避免用戶在填寫訂單資訊時需切換頁面確認商品,將購物車內嵌於結帳頁。
<%= error_messages_for("order") %>
<%= render_component :action => "display_cart", :params => { :context => :checkout } %>
<%= stylesheet_link_tag "scaffold", "depot", :media => "all" %>
:params => { :context => :checkout } 傳遞額外參數給目標動作,讓其區分呼叫情境。
def display_cart
@cart = find_cart
@items = @cart.items
if @items.empty?
redirect_to_index("Your cart is currently empty")
end
if params[:context] == :checkout
render :layout => false # 嵌入時不使用佈局
end
end
- <%= link_to 'Continue shopping', :action => "index" %>
<% unless params[:context] == :checkout -%>
- <%= link_to 'Empty cart', :action => "empty_cart" %>
- <%= link_to 'Checkout', :action => "checkout" %>
<% end -%>
結帳情境下隱藏「清空購物車」與「結帳」連結,避免混淆。
render_component 重用控制器動作與視圖,支援參數傳遞render :layout => false 嵌入模式不套用完整佈局params[:context] 區分獨立/嵌入情境display_cart 一處