为什么我的 Ruby on Rails 更新购物车表单会跳过循环?
我的购物车中的以下代码跳过每个商品的循环,仅输出总数,正确给出购物车的总金额。
有谁知道这是为什么?没有给出任何错误。 我正在使用 Rails 3.1.0 和 Ruby 1.9.2。
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<div class="cart_title">Your Cart</div>
<table>
<% for item in @cart.line_items %>
<% form_for item do |f| %>
<tr>
<td>
<%= f.submit value: 'Update Qty' %>
<%= f.text_field :quantity, size: 1 %>×
</td>
<td class="item_price"><%= number_to_currency(item.total_price) %></td>
</tr>
<% end %>
<% end %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(@cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty cart', @cart, method: :delete,
confirm: 'Are you sure?' %>
Development.log 显示了包含两种产品(每种产品数量 1)的购物车的此活动:
Started GET "/carts/7" for 127.0.0.1 at 2012-02-15 16:23:43 +0000
Processing by CartsController#show as HTML
Parameters: {"id"=>"7"}
Cart Load (0.1ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", "7"]]
LineItem Load (0.1ms) [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = 7
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = 10 LIMIT 1
Product Load (0.1ms) [1mSELECT "products".* FROM "products" WHERE "products"."id" = 11 LIMIT 1
Rendered carts/show.html.erb within layouts/application (41.4ms)
Completed 200 OK in 48ms (Views: 46.5ms | ActiveRecord: 0.9ms)
The following code in my cart is skipping the loop for each item and only outputting the total, correctly giving the overall cart total.
Does anyone know why this is? There is not any error given.
I am using Rails 3.1.0, and Ruby 1.9.2.
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<div class="cart_title">Your Cart</div>
<table>
<% for item in @cart.line_items %>
<% form_for item do |f| %>
<tr>
<td>
<%= f.submit value: 'Update Qty' %>
<%= f.text_field :quantity, size: 1 %>×
</td>
<td class="item_price"><%= number_to_currency(item.total_price) %></td>
</tr>
<% end %>
<% end %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(@cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty cart', @cart, method: :delete,
confirm: 'Are you sure?' %>
Development.log shows this activity for a cart with two products in it (quantity 1 of each):
Started GET "/carts/7" for 127.0.0.1 at 2012-02-15 16:23:43 +0000
Processing by CartsController#show as HTML
Parameters: {"id"=>"7"}
Cart Load (0.1ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", "7"]]
LineItem Load (0.1ms) [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = 7
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = 10 LIMIT 1
Product Load (0.1ms) [1mSELECT "products".* FROM "products" WHERE "products"."id" = 11 LIMIT 1
Rendered carts/show.html.erb within layouts/application (41.4ms)
Completed 200 OK in 48ms (Views: 46.5ms | ActiveRecord: 0.9ms)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要对 form_for 使用
<%=
,例如:这可能是循环中不显示行的原因。
You need to use
<%=
for form_for like:It might be the cause of not showing the rows in the loop.