【问题标题】:Template is missing error even if I have done a redirect_to in the controller即使我在控制器中完成了 redirect_to 模板丢失错误
【发布时间】:2013-07-06 16:30:56
【问题描述】:

我刚从 Agile Web Development With Rails 开始学习 Ruby on Rails。

做一个购物车应用程序,并在实现“空车”功能的过程中。

<%= 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 但它给了我一个错误。

模板丢失 缺少模板 carts/destroy,application/destroy with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :咖啡]}。在以下位置搜索:*“/Users/anil20787/workspace/railsdir/depot/app/views”

这是 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

我确实看过与我的问题类似的 thisthis 帖子。但我无法弄清楚需要做什么。

对于解决此问题的任何帮助表示感谢。

编辑: - 在 redirect_to 周围添加了 {} - 添加了“rake routes”输出

【问题讨论】:

  • 您是否缺少重定向周围的 { }?
  • 在重定向周围使用了 {}。没有帮助。错误仍然存​​在。
  • 你能在 rake 路由后显示输出吗?
  • @AnkitG 已将其添加到帖子中

标签: ruby-on-rails


【解决方案1】:

好的。在遵循教程书时,我在理解工作方面犯了一个错误并忽略了一些细节。下面是我的 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 视图并导致缺少视图异常。

【讨论】:

    【解决方案2】:

    尝试使用以下语法:

    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
    

    【讨论】:

    • 您的问题解决了吗?我不明白为什么上述方法不起作用:(
    • 我得到了帮助。我已经发布了引发错误的原因。
    猜你喜欢
    • 2013-11-03
    • 1970-01-01
    • 2014-05-23
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多