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
这将读取二进制文件,而不是文本