Skip to content

Commit

Permalink
Handles 404 with a custom page using the app template
Browse files Browse the repository at this point in the history
To have a nicer response to visitors of 404s, versus the static one, so user 
can still use the nav/search and continue to other pages on the site.
  • Loading branch information
MacTwister committed Jan 15, 2024
1 parent 934c742 commit c9a6611
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ApplicationController < ActionController::Base

before_action :sentry_user_context, if: :current_user

rescue_from ActiveRecord::RecordNotFound, :with => :error_not_found

def require_login
if current_user.nil?
redirect_to signin_url(goto: request.path), flash: { error: "You must first sign in to access this page" }
Expand All @@ -24,6 +26,13 @@ def require_superadmin

around_action :user_time_zone, if: :current_user

def error_not_found
respond_to do |format|
format.html { render "/application/errors/404", :status => :not_found }
format.all { head :not_found }
end
end

private

def user_time_zone(&block)
Expand Down
15 changes: 10 additions & 5 deletions app/controllers/labs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class LabsController < ApplicationController

# authorize_actions_for Lab, actions: { map: :read, manage_admins: :update}

rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

def embed
@labs = Lab.with_approved_state
# render :embed, layout: false
Expand Down Expand Up @@ -75,11 +77,7 @@ def create
end

def show
begin
@lab = with_approved_or_pending_state(params[:id])
rescue ActiveRecord::RecordNotFound
return redirect_to root_path, notice: 'Lab not found'
end
@lab = with_approved_or_pending_state(params[:id])
# @people = [@lab.creator]
@employees = @lab.employees.includes(:user).active.order('employees.id ASC')
@machines = @lab.machines.includes(:brand, :tags)
Expand Down Expand Up @@ -135,6 +133,13 @@ def allow_iframe
#  From http://jerodsanto.net/2013/12/rails-4-let-specific-actions-be-embedded-as-iframes/
end

def record_not_found
respond_to do |format|
format.html { render "404", :status => :not_found }
format.all { head :not_found }
end
end

def lab_params
params.require(:lab).permit(
:activity_status,
Expand Down
7 changes: 7 additions & 0 deletions app/views/application/errors/404.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.jumbotron.text-center
= title "Page Not Found - 404 error", class: "text-danger"

%br

%p Sorry, the page you were looking for could not be found.

11 changes: 11 additions & 0 deletions app/views/labs/404.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.jumbotron.text-center
= title "Lab Not Found - 404 error", class: "text-danger"

%br

%p Unable to find any Lab matching the URL provided. Try searching or browse through the list of labs.

%br

= link_to t("views.header.labs"), labs_path, class: "btn btn-primary btn-lg"

2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@

root to: 'static#alt'

match '*unmatched', to: 'application#error_not_found', via: :all

end

constraints(ApiSubdomain) do
Expand Down

0 comments on commit c9a6611

Please sign in to comment.