我使用关注
module ErrorHandling
extend ActiveSupport::Concern
included do
rescue_from ActiveRecord::RecordNotFound, with: :notfound
private
def notfound(exception)
logger.warn exception
render file: 'public/404.html', status: :not_found, layout: false
end
end
end
并将其包含在应用程序类中,包括 ErrorHandling 但它不渲染文件:'public/404.html' 有什么问题?
我竭尽全力解决这个问题,但无能为力
将相对路径名传递给
render: file
时,Raiuls 将抛出 ArgumentError
并显示以下消息:
`render file:` should be given the absolute path to a file. 'public/404.html' was given instead
此处传递相对路径的功能在 Rails 6.0 中已被弃用,最终在 Rails 6.1.0 中删除了。
要渲染原始文件,您应该遵循 当前文档:
Rails 可以从绝对路径渲染原始文件。这对于 有条件地渲染静态文件,例如错误页面。
render file: "#{Rails.root}/public/404.html", layout: false
这将呈现原始文件(它不支持 ERB 或其他处理程序)。 默认情况下,它在当前布局内呈现。
警告:将
选项与用户输入结合使用可能会导致安全问题,因为攻击者可以使用此操作 访问文件系统中的安全敏感文件。:file