diff --git a/app/controllers/public/api/v1/portals/articles_controller.rb b/app/controllers/public/api/v1/portals/articles_controller.rb index bc5f6c0fd37d..4e967cfbb1de 100644 --- a/app/controllers/public/api/v1/portals/articles_controller.rb +++ b/app/controllers/public/api/v1/portals/articles_controller.rb @@ -39,10 +39,6 @@ def set_category ) end - def portal - @portal ||= Portal.find_by!(slug: permitted_params[:slug], archived: false) - end - def list_params params.permit(:query, :locale, :sort, :status) end diff --git a/app/controllers/public/api/v1/portals/base_controller.rb b/app/controllers/public/api/v1/portals/base_controller.rb index d9fec27b108b..70aa1e92eded 100644 --- a/app/controllers/public/api/v1/portals/base_controller.rb +++ b/app/controllers/public/api/v1/portals/base_controller.rb @@ -18,6 +18,10 @@ def set_color_scheme end end + def portal + @portal ||= Portal.find_by!(slug: params[:slug], archived: false) + end + def set_locale(&) switch_locale_with_portal(&) if params[:locale].present? switch_locale_with_article(&) if params[:article_slug].present? @@ -40,6 +44,8 @@ def switch_locale_with_portal(&) def switch_locale_with_article(&) article = Article.find_by(slug: params[:article_slug]) + Rails.logger.info "Article: not found for slug: #{params[:article_slug]}" + render_404 && return if article.blank? @locale = if article.category.present? article.category.locale @@ -53,4 +59,9 @@ def switch_locale_with_article(&) def allow_iframe_requests response.headers.delete('X-Frame-Options') if @is_plain_layout_enabled end + + def render_404 + portal + render 'public/api/v1/portals/error/404', status: :not_found + end end diff --git a/app/controllers/public/api/v1/portals/categories_controller.rb b/app/controllers/public/api/v1/portals/categories_controller.rb index 7326f383b5d9..c3a7b59e938a 100644 --- a/app/controllers/public/api/v1/portals/categories_controller.rb +++ b/app/controllers/public/api/v1/portals/categories_controller.rb @@ -13,10 +13,9 @@ def show; end private def set_category - @category = @portal.categories.find_by!(locale: params[:locale], slug: params[:category_slug]) - end + @category = @portal.categories.find_by(locale: params[:locale], slug: params[:category_slug]) - def portal - @portal ||= Portal.find_by!(slug: params[:slug], archived: false) + Rails.logger.info "Category: not found for slug: #{params[:category_slug]}" + render_404 && return if @category.blank? end end diff --git a/app/views/public/api/v1/portals/error/404.html.erb b/app/views/public/api/v1/portals/error/404.html.erb new file mode 100644 index 000000000000..5262e817ee8e --- /dev/null +++ b/app/views/public/api/v1/portals/error/404.html.erb @@ -0,0 +1,12 @@ +
+
+ 🔍 +
+

<%= I18n.t('public_portal.404.title') %>

+

<%= I18n.t('public_portal.404.description') %>

+
+ + <%= I18n.t('public_portal.404.back_to_home') %> + +
+
diff --git a/config/locales/en.yml b/config/locales/en.yml index 8bb388897d4d..e08681c22c1c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -220,6 +220,10 @@ en: made_with: Made with header: go_to_homepage: Go to the main site + 404: + title: Page not found + description: We couldn't find the page you were looking for. + back_to_home: Go to home page slack_unfurl: fields: name: Name