Reputation: 151
I just started learning Ruby on Rails from Agile Web Development With Rails.
Doing a shopping cart application and in the process of implementing the "Empty Cart" feature.
<%= button_to 'Empty cart', @cart, method: :delete,
data: { confirm: 'Are you sure?' } %>
Clicking on the 'Empty Cart' button calls the destroy action in the carts controller
# DELETE /carts/1
# DELETE /carts/1.json
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 I redirect it to the store_url but instead it gives me 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"
Here is the output for rake routes
Prefix Verb URI Pattern Controller#Action
line_items GET /line_items(.:format) line_items#index
POST /line_items(.:format) line_items#create
new_line_item GET /line_items/new(.:format) line_items#new
edit_line_item GET /line_items/:id/edit(.:format) line_items#edit
line_item GET /line_items/:id(.:format) line_items#show
PATCH /line_items/:id(.:format) line_items#update
PUT /line_items/:id(.:format) line_items#update
DELETE /line_items/:id(.:format) line_items#destroy
carts GET /carts(.:format) carts#index
POST /carts(.:format) carts#create
new_cart GET /carts/new(.:format) carts#new
edit_cart GET /carts/:id/edit(.:format) carts#edit
cart GET /carts/:id(.:format) carts#show
PATCH /carts/:id(.:format) carts#update
PUT /carts/:id(.:format) carts#update
DELETE /carts/:id(.:format) carts#destroy
store_index GET /store/index(.:format) store#index
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PATCH /products/:id(.:format) products#update
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
store GET / store#index
I did look at this and this posts which are similar to my problem. But I could not figure out what needs to be done.
Any help in fixing this issue is appreciated.
Edit: - Added {} around redirect_to - Added 'rake routes' output
Upvotes: 3
Views: 7003
Reputation: 151
Ok. I made a mistake in understanding the working and ignored some details while following the tutorial book. Below is my CartsController (partial) and the reason for the error
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
..
# DELETE /carts/1
# DELETE /carts/1.json
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
private
# Use callbacks to share common setup or constraints between actions.
def set_cart
begin
@cart = Cart.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: 'Invalid cart'
else
respond_to do |format|
format.html # show.html.erb
format.json { render json: @cart }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def cart_params
params[:cart]
end
end
Why the problem:
The helper method "set_cart" should not send back a response by itself on success. If it sends a response the action action will not be executed at all. So it should just try and fetch the current cart and save it in the @cart instance variable. My set_cart method actually tries to send back a response. It is this response attempt that is raising the error.
I have a filter for my controller which invokes 'set_cart" before destroy.The "set_cart" method tries to send back a HTML response and in the course searches for a destroy.html.erb view which is not present and results in missing view exception.
Upvotes: 3
Reputation: 17631
Try using the follwoing syntax:
class CartsController < ApplicationController
respond_to :html, :json
def destroy
@cart = current_cart
@cart.destroy
@session[:cart_id] = nil
flash[:notice] = "Your cart is currently empty"
respond_with @cart
end
end
Upvotes: 0