我使用关注
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
此处传递相对路径的能力是deprecated终于在 Rails 6.0 中 removed使用 Rails 6.1.0。
要渲染原始文件,您应该遵循 the current documentation :
Rails can render a raw file from an absolute path. This is useful for conditionally rendering static files like error pages.
render file: "#{Rails.root}/public/404.html", layout: false
This renders the raw file (it doesn't support ERB or other handlers). By default it is rendered within the current layout.
WARNING: Using the
:file
option in combination with users input can lead to security problems since an attacker could use this action to access security sensitive files in your file system.
关于ruby-on-rails - 为什么 Ruby on Rails 7.x.x 不呈现公共(public)文件夹?不是网站图标或 404.html 页面。我使用我的模块,但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77681540/