0

我正在从“Agile web development with rails, 4th edtion”一书中学习 ruby​​。我正在使用 rails 3.2 和 ruby​​ 1.9.2 。

我尝试使用以下代码调用卡的 AJAX 功能

 <%= button_to 'Add to Cart', line_items_path(product_id: product) , remote: true %>

然后在控制器的“创建”操作方法中为 line_item 添加

 respond_to do |format|
      if @line_item.save
       # format.html { redirect_to @line_item.cart,notice: 'Line item was successfully created.' }
        format.html { redirect_to store_url }
        format.js 

然后在 line_items 视图中创建 create.js.erb 文件并添加内容

$('#cart').html("<%=j render @cart %>");

“购物车”部分是

<div class="cart_title">Your Cart</div>
<table>
  <%= render(cart.line_items) %>

  <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?' %>

“line_item”部分是

<tr>

  <td><%= line_item.quantity %>&times;</td>
  <td><%= line_item.product.title %></td>
  <td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
</tr>

现在,当我在正常刷新页面的情况下运行它时,购物车内容会更新并且工作正常,但是当我使用上面显示的“添加到购物车”按钮执行此操作时,它会触发 AJAX 调用,并且似乎以某种方式控制是未到达 create.js.erb 文件或“line_items”部分。当我在开发日志中看到时,我看到了以下消息。

Started POST "/line_items?product_id=1" for 127.0.0.1 at 2012-07-24 22:04:24 +0530
Processing by LineItemsController#create as JS
  Parameters: {"authenticity_token"=>"+8jnhCPyLhj26JDuaP1LRUNhVGGlOpFyJcfVXoffX+U=", "product_id"=>"1"}
  [1m[35mCart Load (0.2ms)[0m  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 14]]
  [1m[36mProduct Load (0.1ms)[0m  [1mSELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1[0m  [["id", "1"]]
  [1m[35mLineItem Load (0.3ms)[0m  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = 14 AND "line_items"."product_id" = 1 LIMIT 1
  [1m[36m (0.1ms)[0m  [1mbegin transaction[0m
  [1m[35m (0.8ms)[0m  UPDATE "line_items" SET "quantity" = 18, "updated_at" = '2012-07-24 16:34:24.620944' WHERE "line_items"."id" = 40
  [1m[36m (12.1ms)[0m  [1mcommit transaction[0m
  [1m[35mLineItem Load (0.2ms)[0m  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = 14
  [1m[36mProduct Load (0.2ms)[0m  [1mSELECT "products".* FROM "products" WHERE "products"."id" = 1 LIMIT 1[0m
  Rendered line_items/_line_item.html.erb (5.3ms)
  Rendered carts/_cart.html.erb (7.2ms)
  Rendered line_items/create.js.erb (8.4ms)
Completed 500 Internal Server Error in 30ms

ActionView::Template::Error (undefined method `title' for nil:NilClass):
    1: <tr>
    2: 
    3:   <td><%= line_item.quantity %>&times;</td>
    4:   <td><%= line_item.product.title %></td>
    5:   <td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
    6: </tr>
    7: <!-- <%= debug(line_item) %> -->
  app/views/line_items/_line_item.html.erb:4:in `_app_views_line_items__line_item_html_erb__4463614436040447986_70300167607120'
  app/views/carts/_cart.html.erb:3:in `_app_views_carts__cart_html_erb___214977825009850242_70300164957540'
  app/views/line_items/create.js.erb:3:in `_app_views_line_items_create_js_erb___2733752682449914066_70300166787700'
  app/controllers/line_items_controller.rb:52:in `create'

请帮助指出错误?谢谢

4

1 回答 1

2

您将获得一个没有产品的 line_item。因此,当你这样做时,你line_item.product会得到一个nil然后,当你这样做时nil.title会抛出一个错误,因此不会渲染视图。也许您忘记在创建操作中获取(正确的)line_item?

于 2012-07-24T18:54:12.383 回答