簡體   English   中英

Rescue_from不起作用

[英]Rescue_from is not working

加載圖像時我試圖處理路由錯誤,但缺少一些圖像。

您知道我只想用默認的圖像圖標替換丟失的圖像並消除錯誤消息。

所以我嘗試了

class ImagesController < ApplicationController
       [...]

       def index
         images = Image.all
         rescue_from ActionController::RoutingError, with: :image_route_error
       end

      [...]
 end

然后我得到了這個:

NoMethodError (undefined method `rescue_from' for #<ImagesController:0x007fe382227e38>
Did you mean?  rescue_handlers):

有任何想法嗎?

您可以使用rescue_from方法從服務器錯誤以外的任何種類的resurance_from異常。 您可以在ApplicationController編寫此方法。

rescue_from ActionController::RoutingError do |exception|
    if controller_name == "image" && action_name == "index"
           render 'default_image_here', status: 200 
    else
     render plain: 'Not found', status: 400 
   end
end

render 'default_image_here'您可以使用以下命令:

render :text => open(image_url, "rb").read, status: 200

這會將文件讀取為二進制文件而不是文本文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM