ruby-on-rails - 为什么 Ruby on Rails 7.x.x 不呈现公共(public)文件夹?不是网站图标或 404.html 页面。我使用我的模块,但它不起作用

标签 ruby-on-rails ruby

我使用关注

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/

相关文章:

ruby-on-rails - guard/listen gem 在控制台上工作

ruby-on-rails - 分页无法正常工作 - will_paginate,bootstrap4

ruby-on-rails - 错误 : While executing gem . ..(ArgumentError)IPv4 地址需要 4 个字节但 1 个字节 - Ruby On Rails

ruby-on-rails - Authlogic 忽略密码参数

sql - 来自复杂联接、求和和组查询的 Rails ActiveRecord 友好代码

ruby-on-rails - Heroku 不重置数据库

ruby-on-rails - RoR 问题 : Where's script/server?

ruby-on-rails - 如何在 Rails 4 中使用 @font-face 嵌入字体?

ruby - 如何优化代码 - 它有效,但我知道我错过了很多学习

ruby - 打印变量