How to access controller code from HTML 404 page in Ruby? [closed]

Multi tool use
How to access controller code from HTML 404 page in Ruby? [closed]
I need to execute a few lines of Ruby in the Rails default 404 page.
Is that possible?
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2 Answers
2
I think you cannot do this. 'Cause 404 page is completely out of rails controllers scope. I would recommend to create errors handling controller (something like 'ErrorsController') and add this line to your routes.rb file, to redirect any bad request to this controller:
get '404', to 'errors#page_404'
get '*path' => redirect('404') unless Rails.env.development?
Is it possible to read the property for the config file in that HTML page
– user3263117
Jul 2 at 16:22
I believe yes - you can write
<%= Settings.your.property.path %>
your property from settings.yml.– Jokūbas Brunonas Pučinskas
Jul 2 at 16:23
<%= Settings.your.property.path %>
Do you mean from .env.properties file?
– EnthuDev
Jul 2 at 16:28
Oh, wait. I didn't understand your last question correctly. Can you specify what exactly you want to read ?
– Jokūbas Brunonas Pučinskas
Jul 2 at 16:36
It is not possible on the default error pages in the public directory.
You can create an errors controller then tell Rails to serve those views instead.
$ rails g controller errors not_found
class ErrorsController < ApplicationController
def not_found
render(status: 404)
end
end
Then you can match all routes with /404 (in routes.rb)
match "/404", :to => "errors#not_found", :via => :all
Then tells Rails to use your matching routes in application.rb ...
config.exceptions_app = self.routes
If you want to make it look like the default 404 page just copy over the static 404 file, which includes styles, then delete it.
Now you can use not_found.html.erb to execute ruby.
If I delete those default static 404 file, what will be the impact.
– user3263117
Jul 2 at 17:16
There won't be any impact because we are telling rails not to use it anymore. However, this will impact 422 and 500 errors, but you can just add them to your routes and errors controller/views in the same manner.
– Mark Merritt
Jul 2 at 17:21
Is there any way to read the values from the property files in that 404 page?
– user3263117
Jul 2 at 16:07