2013-07-09 69 views
3

我剛剛從敏捷Web開發的Rails中學習Ruby on Rails。即使我在控制器中完成了redirect_to,模板仍然缺少錯誤

做一個購物車應用程序,並在實施「空購物車」功能的過程中。

<%= button_to 'Empty cart', @cart, method: :delete, 
data: { confirm: 'Are you sure?' } %> 

點擊「清空購物車」按鈕調用在車控制器

# 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 

在這裏,我重定向到store_url而是它給了我一個錯誤的破壞行動。

模板丟失 缺少模板推車/破壞,應用/使用{破壞:區域設置=> [:EN]:格式=> [:HTML]:處理程序=> [:ERB,:助洗劑, :raw,:ruby,:jbuilder,:coffee]}。搜查:* 「/用戶/ anil20787 /工作區/ railsdir /庫/應用/視圖」

這裏是輸出耙路線

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 

我沒看thisthis職位這與我的問題類似。但我無法弄清楚需要做什麼。

任何幫助解決這個問題表示讚賞。

編輯: - 增加{}周圍redirect_to的 - 增加了 '耙路線' 輸出

+1

是否缺少了{}重定向身邊? – sevenseacat

+0

使用{}周圍重定向。於事無補。錯誤仍然存​​在。 – bobdiya

+0

u能表明耙路後輸出? – AnkitG

回答

3

確定。我在理解工作方面犯了一個錯誤,並忽略了一些細節,同時遵循教程手冊。下面是我CartsController(部分)和錯誤

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 

爲什麼問題的原因:

輔助方法「set_cart」不應由本身的成功發回的響應。如果它發送響應,則動作動作根本不會被執行。所以它應該嘗試獲取當前購物車並將其保存在@cart實例變量中。我的set_cart方法實際上試圖發回一個響應。正是這種提升錯誤的反應嘗試。

我有一個過濾器,用於在銷燬前調用「set_cart」的控制器。「set_cart」方法嘗試發回HTML響應,並在課程中搜索不存在的destroy.html.erb視圖和結果在缺少視圖例外。

0

使用follwoing語法嘗試:

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 
+0

嘗試。錯誤仍然存​​在 – bobdiya

+0

你有沒有設法解決你的問題?我不明白爲什麼上述不會工作:( –

+0

我得到了幫助,我發佈了錯誤提出的原因。 – bobdiya

相關問題