From c9a6611a9355c24cb50b3b267cf78a966b4f00cd Mon Sep 17 00:00:00 2001 From: Julian Gallimore Date: Mon, 15 Jan 2024 10:19:15 +0100 Subject: [PATCH] Handles 404 with a custom page using the app template 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. --- app/controllers/application_controller.rb | 9 +++++++++ app/controllers/labs_controller.rb | 15 ++++++++++----- app/views/application/errors/404.html.haml | 7 +++++++ app/views/labs/404.html.haml | 11 +++++++++++ config/routes.rb | 2 ++ 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 app/views/application/errors/404.html.haml create mode 100644 app/views/labs/404.html.haml diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 058bbb99..6ae78383 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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" } @@ -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) diff --git a/app/controllers/labs_controller.rb b/app/controllers/labs_controller.rb index f2149f61..a844bd43 100644 --- a/app/controllers/labs_controller.rb +++ b/app/controllers/labs_controller.rb @@ -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 @@ -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) @@ -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, diff --git a/app/views/application/errors/404.html.haml b/app/views/application/errors/404.html.haml new file mode 100644 index 00000000..26bae811 --- /dev/null +++ b/app/views/application/errors/404.html.haml @@ -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. + diff --git a/app/views/labs/404.html.haml b/app/views/labs/404.html.haml new file mode 100644 index 00000000..f840aea6 --- /dev/null +++ b/app/views/labs/404.html.haml @@ -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" + diff --git a/config/routes.rb b/config/routes.rb index e737230b..0c5d1399 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -162,6 +162,8 @@ root to: 'static#alt' + match '*unmatched', to: 'application#error_not_found', via: :all + end constraints(ApiSubdomain) do