Search code examples
ruby-on-railsrubymine

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


I am learning Agile Web Development with Rails(4th), undergoing iteration E3: Finishing the cart. When trying to add a empty cart button, I get an error

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"

Basically, i get the exact same code as this question asked.

So what I did was, trying to edit the set_cartpart which is underbefore action. Unfortunately, it didn't work out.

Therefore I created a carts.destroy.html.erb file as wanted, and I add a redirecting to my store_url in this html, here is the content:

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

And it directs to my store as expected, the PROBLEM is the cart is NOT destroyed. And when I add a new items in the cart, it was still the old cart.

For convenience, here is destroy in cart_controller:

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

here is empty cart button:

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

Maybe in my cart_controller, the session deleting didn't work? Or how to make empty cart work?

Really appreciate your help!!

Here is the server log file:

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)

Solution

  • Filter chain halted as :set_cart rendered or redirected

    Main issue is here. You can set it to as follows:

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

    Using this set cart method call only before show and edit actions.