2017-09-17 67 views
0

在開發的過程中的應用,我想出了一個絆腳石:Rails的呈現狀態:沒有發現失蹤模板錯誤

The error screen

這裏是我的股票控制器錯誤,其中出現的錯誤:

class StocksController < ApplicationController 
    def search 
    if params[:stock] 
     @stock = Stock.find_by_ticker(params[:stock]) 
     @stock ||= Stock.new_from_lookup(params[:stock]) 
    end 

    if @stock 
     render json: @stock 
     #render partial: 'lookup' 

    else 
     render status: :not_found ,nothing: true 
    end 

    end 

end 

在球場上,他們有相同的代碼,我做的,但對他們來說,這只是works.The我所知道的是,他們正在軌道4(氧化亞氮)的區別,那我使用Rails 5(Mac OS X/Atom IDE/GitLab存儲庫)。請儘可能幫助我!謝謝!你提前!

回答

2

:nothing選項是deprecated並將在Rails 5.1中刪除。使用head方法以空響應主體進行響應。

試試這個:

render body: nil, status: :not_found 

或:

head :not_found 

請不要發佈錯誤圖像,複製過去的文本

+0

它的工作!非常感謝! – ds998

0

這裏的問題是,你是在else子句中不會呈現json,因此Rails將查找不存在的HTML視圖。若要解決此問題,請更新代碼如下:

class StocksController < ApplicationController 
    def search 

    if params[:stock] 
     @stock = Stock.find_by_ticker(params[:stock]) 
     @stock ||= Stock.new_from_lookup(params[:stock]) 
    end 

    if @stock 
     render json: @stock 
     #render partial: 'lookup' 

    else 
     render :json => {:error => "not-found"}.to_json, :status => 404 
    end 

    end 

end 
+0

謝謝你的解決方案,它出色地工作! – ds998