What to do if the image posted by refile disappears after setting a 404 error page in Rails

Overview

Only when I create a 404 error page in Rails, the image posted by refile disappears. If you comment out the code on the error page, the image will be displayed.

The code for the 404 error page is below.

application_controller.rb


  unless Rails.env.development?
    rescue_from Exception,                      with: :render_500 
    rescue_from ActiveRecord::RecordNotFound,   with: :render_404
    rescue_from ActionController::RoutingError, with: :render_404
  end

#Create an error directory with views and 404 there.html.erb/505.html.Create erb
  def render_404
    render 'error/404', status: :not_found
  end

  def render_500
    render 'error/500', status: :internal_server_error
  end

config/routes.rb


Rails.application.routes.draw do

#It will be evaluated in order from the top, so write it at the bottom
get '*path', to: 'application#render_404'

end

Cause

If you check rails routes in the terminal,

GET /*path(.:format)
refile_app /attachments

It was in the order of. Since routes are evaluated from above, get'* in config / routes.rbis evaluated before/ attachments, which is the URL of the refile with nothing set in config / routes.rb, is evaluated. path', to:'application # render_404' has been evaluated.

Coping

It's okay to evaluate refile_app / attachments beforeGET / * path (.: Format).

First, add the following to ʻinitializers / refile.rb. If refile.rb` does not exist, create a new one.

config/initializers/refile.rb


Refile.automount = false

config/routes.rb


Rails.application.routes.draw do

mount Refile.app, at: Refile.mount_point, as: :refile_app #add to
get '*path', to: 'application#render_404' #Write at the bottom

end

If you check rails routes in the terminal,

refile_app /attachments
GET /*path(.:format)

I think it is in the order of.

I think that / attachments will be evaluated first and the image will be displayed.

https://github.com/refile/refile#mounting As I wrote in, I had to turn off the automatic mounting of refile and set the route myself.

Recommended Posts

What to do if the image posted by refile disappears after setting a 404 error page in Rails
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
[Rails / Docker] What to do if access is denied by the browser (localhost: 3000) after adding a gem
What to do if you get a gcc error in Docker
What to do if Cloud9 is full in the Rails tutorial
What to do if you get a DISPLAY error in gym.render ()
What to do if you get a "302" error in your controller unit test code in Rails
What to do if you get a "Mysql2 :: Error: Operand should contain 1 column (s)" error in Rails
What to do if the background image is not applied after deployment
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
What to do if the Rails server can't start
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if the update does not take effect after deploying Rails AWS
What to do if you get a wrong number of arguments error in binding.pry
What to do if you get a java.io.IOException in GlassFish
What to do if mysql2 gets a bundle install error
What to do if you can't use the rails command
What to do if you don't see the test code error message in the terminal console
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
[Rails] What to do if you can't get an error message with the errors method
What to do if you get an error in Basic authentication during Rails test code
[Rails 6] What to do when a missing a template error occurs after introducing haml [Super easy]
[Rails] What to do if data is not registered in DB
What to do if you forget the root password in CentOS7
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
[Programming beginner] What to do when rails s becomes an error in the local development environment
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do if you get a groovy warning in Thymeleaf Layout
What to do if you get an error during rails db: reset
What to do if the prefix c is not bound in JSP
What to do if you don't like the code generated by swagger-codegen-cli
What to do if you get an error with bundle install by entering gem'bcrypt' in your Gemfile
What to do if Operation not permitted is displayed when you execute a command in the terminal
[Gradle] [checkstyle] What to do if the active setting of Checkstyle is removed by "Refresh Gradle project" from Eclipse
What to do if an error occurs in VS Code when importing a django module or your own module installed by pip install
What to do if the build fails with the error "Module compiled with Swift x.x.x cannot be imported by the Swift x.x.x compiler"
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do if you can't activate the select box created by bootstrap-select
What to do if you get the error message unrecognized selector send to instance "***"
What to do if the changes are not reflected in the jar manifest file
What to do when rails db: seed does not reflect in the database
What to do if you get a javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake in the IBM JDK
What to do if you get Could not save master table to file after importing a project in Eclipse
Androd: What to do about "The Realm is already in a write transaction in"
What to do if the rails server doesn't run out on AWS cloud9
What to do if ffi installation fails when launching an application in Rails
How to translate the error message into Japanese (What to do if you cannot log in for some reason)
What to do about "A server is already running ..." that happened without turning off the rails server in the terminal
What to do if you get an error saying "Please enter a valid value" when getting with Rails datetime_field
What to do if you get an error saying "Could not find a JavaScript runtime." When starting rails server
What to do if you get the warning "Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1." in Rails 6.0
What to do if you can't find the JDK you put in SDKMAN after migrating from bash to zsh on macOS
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
What to do if Failure / Error: require File.expand_path ('../ config / environment', __dir__) appears in RSpec
What to do when Address already in use is displayed after executing rails s
What to do if you get the error Couldn't find Item without an ID
What to do if you can't find your API key after deploying to Rails heroku
What to do if you get a port error when docker-compose up on Mac