Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 救援行动不起作用_Ruby On Rails_Controller_Rescue - Fatal编程技术网

Ruby on rails 救援行动不起作用

Ruby on rails 救援行动不起作用,ruby-on-rails,controller,rescue,Ruby On Rails,Controller,Rescue,我试图处理路由错误时,我加载的图像和一些丢失 您知道,我只是想用默认图像图标替换丢失的图像,并抑制错误消息 所以我试过了 class ImagesController < ApplicationController [...] def index images = Image.all rescue_from ActionController::RoutingError, with: :image_route_error

我试图处理路由错误时,我加载的图像和一些丢失

您知道,我只是想用默认图像图标替换丢失的图像,并抑制错误消息

所以我试过了

class ImagesController < ApplicationController
       [...]

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

      [...]
 end
class ImagesController
然后我得到了这个:

NoMethodError (undefined method `rescue_from' for #<ImagesController:0x007fe382227e38>
Did you mean?  rescue_handlers):
NoMethodError(未定义的方法'rescue_from'#
你是说?救命啊

有什么想法吗?

您可以使用
rescue\u from
方法解救除服务器错误以外的任何类型的异常。您可以在
应用程序控制器中编写此方法

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\u image\u here'
中,您可以使用:

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

这将以二进制文件而不是文本的形式读取文件。

您可以使用
rescue\u from
方法从服务器错误以外的任何类型的异常中营救\u。您可以在
应用程序控制器中编写此方法

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\u image\u here'
中,您可以使用:

render :text => open(image_url, "rb").read, status: 200
这将读取二进制文件,而不是文本