2

我是 Rails 的新手(我的第 5 周),目前我正在学习“agile web dev with rails 4”一书。我刚读完第 11 章,自娱自乐,我计划添加一个远程调用来销毁购物车中物品的所有数量。好吧,问题是,我收到内部错误消息 500。

我在文件 line_items/_line_item.html.erb 中添加了代码“remote:true”

...
...
<%= button_to 'Remove Item', line_item, method: :delete, remote: true %>

并将 html 的重定向更改为 store_path 并在 line_items_controller.rb 中添加了 format.js

我创建了一个 destroy.js.erb 文件并添加了代码

$('#cartContainer').html('<%= escape_javascript render(@cart) %>');

我猜@cart 是错误的。但我只是想不通,可能我完全失去了对洞项目的概述:)

这里是服务器日志:

Started DELETE "/line_items/105" for 127.0.0.1 at 2014-08-07 12:44:15 +0200
Processing by LineItemsController#destroy as JS
Parameters: {"authenticity_token"=>"ofobuV7Pk1avFiV6KSQ7NHnc/J77PJWWejsKVyg3YLQ=", "id"=>"105"}
LineItem Load (0.1ms)  SELECT  "line_items".* FROM "line_items"  WHERE "line_items"."id" = ? LIMIT 1  [["id", 105]]
CACHE (0.0ms)  SELECT  "line_items".* FROM "line_items"  WHERE "line_items"."id" = ? LIMIT 1  [["id", "105"]]
(0.1ms)  begin transaction
SQL (0.4ms)  DELETE FROM "line_items" WHERE "line_items"."id" = ?  [["id", 105]]
(1.6ms)  commit transaction
Rendered line_items/destroy.js.erb (0.9ms)
Completed 500 Internal Server Error in 8ms

ActionView::Template::Error ('nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.):
1: // reload cart after adding item
2: $('#cartContainer').html('<%= escape_javascript render(@cart) %>');
app/views/line_items/destroy.js.erb:2:in `_app_views_line_items_destroy_js_erb__1371414078062466935_70243559055940'
app/controllers/line_items_controller.rb:81:in `destroy'
4

1 回答 1

2

您收到此错误是因为@cart 为 nil。在你的控制器中销毁方法初始化@cart

def destroy
  @cart = current_cart
  // other logic
end
于 2014-08-07T11:20:23.713 回答