Empty_Cart 不起作用,购物车会话未被破坏并在视图中继续指向 carts/destroy.html.erb,否则显示为模板丢失

Empty_Cart doesn't work, cart session not destroyed and keeps directing to carts/destroy.html.erb in views, otherwise shown as Template Missing

我正在学习 使用 Rails(4th) 进行敏捷 Web 开发,正在进行 迭代 E3:完成购物车 。尝试添加 empty cart 按钮时,出现错误

Template is missing Missing template carts/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/Users/anil20787/workspace/railsdir/depot/app/views"

基本上,我得到的代码与 this question asked 完全相同。

所以我所做的是,尝试编辑set_cart下面before action的部分。不幸的是,它没有成功。

因此我根据需要创建了一个carts.destroy.html.erb文件,并在这个html中添加了一个重定向到我的store_url,内容如下:

<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url= store_url">

它按预期指向我的 store,问题是购物车没有被毁坏。当我在购物车中添加新商品时,它仍然是旧购物车。

为方便起见,这里是cart_controller中的destroy

def destroy
  @cart = current_cart
  @cart.destroy
  @session[:cart_id] = nil

 respond_to do |format|
  format.html { redirect_to store_url, notice: 'Your cart is currently empty' }
  format.json { head :no_content }
 end
end

这是 empty cart 按钮:

<%=  button_to 'Empty cart', cart_path(@cart), :method => :delete,
 :confirm => 'Are you sure?', :remote=> true %>

也许在我的cart_controller中,会话删除不起作用?或者如何使 empty cart 工作?

非常感谢您的帮助!!

这是服务器日志文件:

Started DELETE "/carts/1" for 127.0.0.1 at 2016-09-28 16:26:04 +0800
Processing by CartsController#destroy as HTML
Parameters:    
{"authenticity_token"=>"3C5rVYL+5WFQZLp5bHquWkYWwp8VbdJXrewHcOM2US4=",   
 "id"=>"1"}
 Cart Load (0.4ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ?  
 LIMIT 1  [["id", "1"]]
Rendered carts/destroy.html.erb within layouts/application (2.0ms)
LineItem Load (0.4ms)  SELECT "line_items".* FROM "line_items" WHERE   
"line_items"."cart_id" = ?  [["cart_id", 1]]
 Product Load (0.5ms)  SELECT "products".* FROM "products" WHERE  
"products"."id" = ? ORDER BY title LIMIT 1  [["id", 2]]
Product Load (0.5ms)  SELECT "products".* FROM "products" WHERE     
"products"."id" = ? ORDER BY title LIMIT 1  [["id", 3]]
 Product Load (0.4ms)  SELECT "products".* FROM "products" WHERE   
 "products"."id" = ? ORDER BY title LIMIT 1  [["id", 1]]
 Rendered line_items/_line_item.html.erb (7.8ms)
 DEPRECATION WARNING: :confirm option is deprecated and will be removed from
 Rails 4.1. Use 'data: { confirm: 'Text' }' instead. (c
 alled from _app_views_carts__cart_html_erb___2408859514053962311_28668160 at
/mnt/c/Users/Alex Xiong/RubymineProjects/work/depot/a
pp/views/carts/_cart.html.erb:13)
Rendered carts/_cart.html.erb (19.3ms)
Filter chain halted as :set_cart rendered or redirected
Completed 200 OK in 157ms (Views: 151.7ms | ActiveRecord: 2.3ms)


Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2016-09-28 16:26:05 +0800


Started GET "/javascripts/defaults.js" for 127.0.0.1 at 2016-09-28 16:26:05 +0800

 ActionController::RoutingError (No route matches [GET]   
 "/javascripts/defaults.js"):
 actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in 
  `call'
 actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in     
 `call'
 railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
 railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
 activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in
 tagged'
 activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
 activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
 railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
 actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in
 `call'
 rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
 rack (1.5.5) lib/rack/runtime.rb:17:in `call'
 activesupport (4.0.2)  
 lib/active_support/cache/strategy/local_cache.rb:83:in `call'
 rack (1.5.5) lib/rack/lock.rb:17:in `call'
 actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
 rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
 railties (4.0.2) lib/rails/engine.rb:511:in `call'
 railties (4.0.2) lib/rails/application.rb:97:in `call'
 rack (1.5.5) lib/rack/lock.rb:17:in `call'
 rack (1.5.5) lib/rack/content_length.rb:14:in `call'
 rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
 /usr/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
 /usr/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
 /usr/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'


 Rendered /var/lib/gems/2.2.0/gems/actionpack-  
 4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.6ms)
 Rendered /var/lib/gems/2.2.0/gems/actionpack-
 4.0.2/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.3ms)
 Rendered /var/lib/gems/2.2.0/gems/actionpack-
 4.0.2/lib/action_dispatch/middleware/templates/routes/_table.html.erb  
 (1.1ms)
 Rendered /var/lib/gems/2.2.0/gems/actionpack-
 4.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.erb 
 within res
 cues/layout (62.0ms)


 Started GET "/carts/store_url" for 127.0.0.1 at 2016-09-28 16:26:06 +0800
 Processing by CartsController#show as HTML
 Parameters: {"id"=>"store_url"}
 Cart Load (0.4ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ?    
 LIMIT 1  [["id", "store_url"]]
 Attempt to access invalid cart store_url
 Redirected to http://localhost:3000/
 Filter chain halted as :set_cart rendered or redirected
 Completed 302 Found in 3ms (ActiveRecord: 0.4ms)


 Started GET "/" for 127.0.0.1 at 2016-09-28 16:26:06 +0800
 Processing by StoreController#index as HTML
 Cart Load (0.4ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 
 1  [["id", 1]]
   Product Load (0.4ms)  SELECT "products".* FROM "products" ORDER BY title
   Rendered store/index.html.erb within layouts/application (4.2ms)
   LineItem Load (0.3ms)  SELECT "line_items".* FROM "line_items" WHERE     
  "line_items"."cart_id" = ?  [["cart_id", 1]]
   Product Load (0.3ms)  SELECT "products".* FROM "products" WHERE                      
   "products"."id" = ? ORDER BY title LIMIT 1  [["id", 2]]     
   Product Load (0.3ms)  SELECT "products".* FROM "products" WHERE  
     "products"."id" = ? ORDER BY title LIMIT 1  [["id", 3]]
   Product Load (0.5ms)  SELECT "products".* FROM "products" WHERE 
 "products"."id" = ? ORDER BY title LIMIT 1  [["id", 1]]
  Rendered line_items/_line_item.html.erb (6.5ms)
 DEPRECATION WARNING: :confirm option is deprecated and will be removed from
  Rails 4.1. Use 'data: { confirm: 'Text' }' instead. (c
 alled from _app_views_carts__cart_html_erb___2408859514053962311_28668160      
 at /mnt/c/Users/Alex Xiong/RubymineProjects/work/depot/a
 pp/views/carts/_cart.html.erb:13)
 Rendered carts/_cart.html.erb (18.5ms)
 Completed 200 OK in 100ms (Views: 95.9ms | ActiveRecord: 2.3ms)


  Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2016-09-28 16:26:06 +0800


  Started GET "/javascripts/defaults.js" for 127.0.0.1 at 2016-09-28 16:26:06 +0800

  ActionController::RoutingError (No route matches [GET] 
 "/javascripts/defaults.js"):
  actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
 activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
 activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
 railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
 actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
 rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
 rack (1.5.5) lib/rack/runtime.rb:17:in `call'
 activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
 rack (1.5.5) lib/rack/lock.rb:17:in `call'
 actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
 rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
 railties (4.0.2) lib/rails/engine.rb:511:in `call'
 railties (4.0.2) lib/rails/application.rb:97:in `call'
 rack (1.5.5) lib/rack/lock.rb:17:in `call'
 rack (1.5.5) lib/rack/content_length.rb:14:in `call'
 rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
 /usr/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
 /usr/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
 /usr/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'


 Rendered /var/lib/gems/2.2.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
 Rendered /var/lib/gems/2.2.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.1ms)
 Rendered /var/lib/gems/2.2.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.6ms)
 Rendered /var/lib/gems/2.2.0/gems/actionpack-   4.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within    res
cues/layout (57.8ms)

过滤器链停止为:set_cart 呈现或重定向

主要问题在这里。您可以将其设置为:

before_action :set_cart ,:only => only: [:show, :edit, :update, :destroy]

仅在显示和编辑操作之前使用此设置购物车方法调用。