Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 920
scottkekoa Avatar asked Sep 08 '25 07:09

scottkekoa


1 Answers

It works for me with File.join:

render file: File.join(Rails.root, 'public', "404.html")
like image 50
LoVka Avatar answered Sep 10 '25 05:09

LoVka