Redirect_to来自destroy不能在rails 4中工作

时间:2014-04-09 10:45:33

标签: ruby-on-rails redirect destroy

我有button_to元素

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

它需要在网上商店中删除Cart的对象。

这里的破坏方法

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

但是当我点击我的按钮Rails说我没有模板&#34; destroy.html.erb&#34;。我创造了它。并且Rails总是将我重定向到此页面。我试着像这样写

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

在我看来,它必须破坏我的@cart然后重定向。但是我开始使用destroy.html,推车还在那里。

我需要你帮助guyz,非常感谢))

UPD:此处的佣金路线屏幕
http://postimg.org/image/y1h1pzpsp/

UPD2:日志在这里: https://docs.google.com/file/d/0B5CgJPSign_YWFhNRWw5bk9hZXc/

UPD3:购物车控制器

#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

1 个答案:

答案 0 :(得分:0)

您的错误来源是set_cart方法。我想你应该删除:

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

一部分。