【发布时间】:2012-05-29 21:45:42
【问题描述】:
我有一个购物车,我想在我的应用中以 3 种不同的方式呈现。
- 在边栏中。仅显示购物车中的商品数量及其总价。
- 在购物车主视图中。显示带有每个项目的产品、数量和总价链接的行项目。还显示增加/减少商品数量的按钮和从购物车中删除商品的按钮。
- 在订单视图中,显示购物车内容的方式与主购物车视图相同,但产品链接、更改数量的按钮和“删除”按钮除外。
到目前为止,我这样渲染购物车:
carts/_cart.html.erb
<%= yield %>
购物车侧边栏布局carts/_sidebar.html.erb
<ul>
<li class="nav-header">Your Cart (<%= pluralize(@cart.total_items, "Item") %>)</li>
<li>Total Due: <%= number_to_euro(@cart.total_price) %></li>
<% unless @cart.line_items.empty? %>
<li><%= link_to "View Cart & Checkout", cart_path(@cart) %></li>
<li><%= link_to "Empty Cart", @cart, :method => :delete %></li>
<% end %>
</ul>
由<%= render :partial => 'carts/cart', :layout => 'carts/sidebar' %>从layouts/_sidebar.html.erb渲染
购物车主布局carts/_main.html.erb
<table>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Subtotal</th>
<th></th>
</tr>
<%= render @line_items %>
<tr id="total_line">
<td colspan="3">Total:</td>
<td><%= number_to_euro(@cart.total_price) %></td>
<td></td>
</tr>
</table>
从carts/show.html.erb渲染
<h1><%= pluralize(@cart.total_items, "Item") %> in Your Cart</h1>
<%= render :partial => 'cart/cart', :layout => 'carts/main' %>
<%= link_to "Empty Cart", @cart, :method => :delete %>
<%= link_to "Checkout", new_order_path %>
还有carts/_order.html.erb,目前从orders/new.html.erb 渲染,与购物车主视图中的方式相同。
我想做的是创建 2 个不同的布局来呈现来自 carts/show.html.erb 和 orders/new.html.erb 的订单项。为此,我在line_items/_line_item.html.erb 中有<%= yield %>
购物车主布局的订单项布局line_items/_main.html.erb
<tr>
<td><%= link_to "#{line_item.product.brand.name} #{line_item.product.title}", product_path(line_item.product) %></td>
<td>
<%= link_to "-", decrement_line_item_path(line_item), :method => :post %>
<%= line_item.quantity %>
<%= link_to "+", increment_line_item_path(line_item), :method => :post %>
</td>
<td><%= number_to_euro(line_item.product.price) %></td>
<td><%= number_to_euro(line_item.total_price) %></td>
<td><%= link_to "Remove"), line_item, :method => :delete %></td>
</tr>
新订单视图的类似订单项布局line_items/_order.html.erb
<tr>
<td><%= "#{line_item.product.brand.name} #{line_item.product.title}" %></td>
<td><%= line_item.quantity %></td>
<td><%= number_to_euro(line_item.product.price) %></td>
<td><%= number_to_euro(line_item.total_price) %></td>
</tr>
这就是问题的开始。我不明白如何呈现集合。我尝试像这样渲染来自carts/_main.html.erb 的订单项
<%= render :partial => 'line_items/line_item', :layout => 'line_items/main', :collection => @line_items %>
像这样来自carts/_order.html.erb
<%= render :partial => 'line_items/line_item', :layout => 'line_items/order', :collection => @line_items %>
但我在 Carts#show 中遇到 LocalJumpError
Showing app/views/line_items/_line_item.html.erb where line #1 raised:
no block given (yield)
任何其他 :collection 名称都不会呈现任何内容。我做错了什么?
【问题讨论】:
标签: ruby-on-rails partial yield