AJAX调用无法在Ruby上运行

时间:2012-07-24 16:57:31

标签: ruby ruby-on-rails-3

我正在从“使用rails,第4版的敏捷网络开发”一书中学习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'

请帮助指向错误?感谢

1 个答案:

答案 0 :(得分:1)

您收到的line_item没有产品。因此,当您执行line_item.product时,您会得到nil,然后,当您执行nil.title时,会抛出错误,因此不会呈现视图。也许你忘了在创建动作中获取(正确的)line_item?