为什么我的 Ruby on Rails 更新购物车表单会跳过循环?

发布于 2025-01-05 10:03:41 字数 1726 浏览 0 评论 0原文

我的购物车中的以下代码跳过每个商品的循环,仅输出总数,正确给出购物车的总金额。

有谁知道这是为什么?没有给出任何错误。 我正在使用 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 %>&times;
        </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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初相遇 2025-01-12 10:03:42

您需要对 form_for 使用 <%= ,例如:

<%= form_for item do |f| %>

这可能是循环中不显示行的原因。

You need to use <%= for form_for like:

<%= form_for item do |f| %>

It might be the cause of not showing the rows in the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文