Question

I had button_to element

<%= button_to 'Destroy cart', @cart, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger btn-mini" %>

it needs to delete a object of Cart in online-store.

Here's destroy method

def destroy
  @cart = current_cart
  @cart.destroy
  session[:cart_id] = nil
  respond_to do |format|
    format.html { redirect_to store_url, notice: "Now your cart is empty." }
    format.json { head :ok }
  end
end

But when I clicked my button Rails said that I have no template "destroy.html.erb". I created it. And Rails always redirect me to this page. I tried to write redirect in the end like this

respond_to do |format|
    format.html { notice: "Now your cart is empty." }
    format.json { head :ok }
  end
redirect_to store_url

In my mind it must destroy my @cart and then redirect. But I get on destroy.html, and cart still there.

I need you help guyz, thanks a lot))

UPD: Here's screen of rake routs
http://postimg.org/image/y1h1pzpsp/

UPD2: Log is here: https://docs.google.com/file/d/0B5CgJPSign_YWFhNRWw5bk9hZXc/

UPD3: Cart Contoller

#encoding: utf-8
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]

# GET /carts
# GET /carts.json
def index
  @carts = Cart.all
end

# GET /carts/1
# GET /carts/1.json
def show

end

# GET /carts/new
def new
  @cart = Cart.new
end

# GET /carts/1/edit
def edit
end

# POST /carts
# POST /carts.json
def create
  @cart = Cart.new(cart_params)

  respond_to do |format|
    if @cart.save
      format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
      format.json { render action: 'show', status: :created, location: @cart }
    else
      format.html { render action: 'new' }
      format.json { render json: @cart.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /carts/1
# PATCH/PUT /carts/1.json
def update
  respond_to do |format|
    if @cart.update(cart_params)
      format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @cart.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /carts/1
# DELETE /carts/1.json
def destroy
  @cart = current_cart
  @cart.destroy
  session[:cart_id] = nil
  redirect_to store_url
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 "Trying get access to null cart."
      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

Was it helpful?

Solution

The source of your error is set_cart method. I guess you should remove:

else
  respond_to do |format|
    format.html # show.html.erb
    format.json {render json: @cart}

part.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top