scottkekoa
scottkekoa

Reputation: 149

Rails 6.1 How to Render A FIle Into A Page Template

This used to work -

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

After updating to Rails 6.1, it no longer works with this error -

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

The problem with the absolute path is doesn't insert the file into my page properly...so I settled on this -

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

That works but...it will no let me use any Rails ERB code like this -

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

Anyone have any idea on how to make this work?

Thanks

Upvotes: 4

Views: 1642

Answers (2)

LoVka
LoVka

Reputation: 41

It works for me with File.join:

render file: File.join(Rails.root, 'public', "404.html")

Upvotes: 4

gertas
gertas

Reputation: 17145

Try this:

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

In case HTML tags get escaped then use <%== or add .html_safe on render result.

Upvotes: 0

Related Questions