Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static 404 page Rails 4: how to use the asset pipeline?

I'm using Rails 4.2.3 and am trying to customize the 404 error page in public/404.html. How can I include images from the asset pipeline?

There's an excellent post how to build dynamic custom error pages. However, as described there, it requires a whole lot of changes to settings that I, as a beginner, am not ready to take. All I want to do is in my 404 page to include 2 images that are in the asset pipeline. Is there an easy way to do this?

like image 621
Marty Avatar asked Jul 17 '15 20:07

Marty


2 Answers

If you want your error page to use images from the asset pipeline, then you have two options:

  1. Use dynamic error pages (I've written a tutorial here).
  2. Monkey patch the asset pipeline to allow non-fingerprinted assets.

Since you are ruling out option #1 for now, I think the monkey patch is the way to go. Install the non-stupid-digest-assets gem in your app. This will patch the asset pipeline so that it produces non-fingerprinted assets (in addition to the fingerprinted ones).

# Gemfile
gem "non-stupid-digest-assets"

And of course, don't forget:

$ bundle install

Then in your 404.html, just refer to the asset as if it were a static file, like this:

<img src="/assets/my-image.png">

This assumes the actual image is stored here in your project:

app/assets/images/my-image.png
like image 140
Matt Brictson Avatar answered Sep 20 '22 23:09

Matt Brictson


Keep in mind, that content of public folder is visible to anyone. There for, following the convention, I would create assets folder (if you don't have it already), then images and stylesheets. And create regular html page

- app
...
- public
  - 404.html
  - images
    - image1.jpg
    - image2.jpg
  - stylesheets
    - style.css

Then in your 404.html you will reference it like so:

<img src="./assets/images/image1.jpg" alt=""/>

like image 36
Pav31 Avatar answered Sep 22 '22 23:09

Pav31