【问题标题】:Rails 6.1 How to Render A FIle Into A Page TemplateRails 6.1 如何将文件渲染到页面模板中
【发布时间】:2021-07-28 04:29:50
【问题描述】:

这曾经有效 -

render file: "public/404.html", status: :not_found

更新到 Rails 6.1 后,它不再出现此错误 -

render file: should be given the absolute path to a file. 'public/404.html' was given instead

绝对路径的问题是没有正确地将文件插入到我的页面中......所以我决定了 -

render html: Rails.public_path.join('404.html.erb').read.html_safe, status: :not_found, layout: 'application'

这行得通,但是......它不会让我使用任何这样的 Rails ERB 代码 -

<% content_for :head do %>
  <title>404 Error - Not Found</title>
  <meta name="description" content="404 Error - Not Found">
<% end %>

有人知道如何进行这项工作吗?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-6.1


    【解决方案1】:

    试试这个:

    render file: Rails.root.join("public/404.html"), status: :not_found
    

    如果 HTML 标签被转义,则使用&lt;%== 或在render 结果上添加.html_safe

    【讨论】:

    • 我也试过了,得到同样的错误 - 渲染文件:应该给出文件的绝对路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2011-08-28
    相关资源
    最近更新 更多