繁体   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