I searched for every code that I can find here in stackoverflow on how you actually can redirect the pages that doesn't exist or unknown to a 404 page.
For instance you visited this page http://localhost:3000/thispageisnull since the page doesn't exist it will automatically redirect you to the default 404 custom page I created.
So far here's what I did. On my errors_controller.rb file I place this code:
def show_404
render "404", status: 404
end
And then on my routes.rb file, I place this code:
match '*path', via: :all, to: 'errors#show_404'
This basically works for me as it redirects all of the unknown pages to 404 page. However, it seems it's messing up with the ActiveStorage images path.
Whenever I am placing these codes and redirecting every page that doesn't exist to 404, it's always making my ActiveStorage images broken. As in like this image: https://i.sstatic.net/kD0j0.jpg
And if I remove these piece of codes about the 404 redirection, it always turn back to normal images.
What is the best way to do a redirection to 404 page w/o affecting the ActiveStorage images?
You can try doing something like this which might help, in your routes.rb change the line like this:
match '*path', via: :all, to: 'errors#show_404', constraints: lambda { |req|
req.path.exclude? 'rails/active_storage'
}
So it will exclude that path and let the application show the images. Please change the path as per your requirement (I think it should be rails/active_storage only)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With