Skip to content

Commit

Permalink
custom error pages (404, 500)
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Mar 8, 2023
1 parent 95fef3d commit b6bebd7
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 134 deletions.
Binary file added app/assets/images/404.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/500.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ErrorsController < ActionController::Base
def internal_server_error
render status: 500
end

def not_found
render status: 404
end
end
6 changes: 6 additions & 0 deletions app/views/errors/internal_server_error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class='text-center'>
<h1 class="font-bold text-4xl"><%= response.status %> <%= action_name.humanize %></h1>
<p>Something went wrong</p>
<%= link_to 'Go to homepage', root_url, class: 'text-blue-500' %>
<%= image_tag "#{response.status}.png" %>
</div>
6 changes: 6 additions & 0 deletions app/views/errors/not_found.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class='text-center'>
<h1 class="font-bold text-4xl"><%= response.status %> <%= action_name.humanize %></h1>
<p>This page does not exist</p>
<%= link_to 'Go to homepage', root_url, class: 'text-blue-500' %>
<%= image_tag "#{response.status}.png" %>
</div>
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

<body>
<main class="container mx-auto mt-28 px-5 flex">
header
<%= yield %>
footer
</main>
</body>
</html>
18 changes: 18 additions & 0 deletions app/views/layouts/errors.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>CustomErrorPages</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>

<body>
<main class="grid h-screen place-items-center">
<%= yield %>
</body>
</html>
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module CustomErrorPages
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0

config.exception_app = self.routes
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Rails.application.routes.draw do
match '/404', via: :all, to: 'errors#not_found'
match '/500', via: :all, to: 'errors#internal_server_error'

root 'statis#index'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

Expand Down
67 changes: 0 additions & 67 deletions public/404.html

This file was deleted.

66 changes: 0 additions & 66 deletions public/500.html

This file was deleted.

0 comments on commit b6bebd7

Please sign in to comment.