From c556e9c694a0368548231184e9c6359c4baa3396 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Tue, 20 Sep 2022 23:01:39 +0530 Subject: [PATCH] Fix: Redirect to portal with default locale (#5467) - Redirect to default locale if URL does not provide the locale param --- app/controllers/public/api/v1/portals_controller.rb | 8 ++++++++ app/views/public/api/v1/portals/show.html.erb | 2 +- config/routes.rb | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/controllers/public/api/v1/portals_controller.rb b/app/controllers/public/api/v1/portals_controller.rb index c292198a6..98c54ca35 100644 --- a/app/controllers/public/api/v1/portals_controller.rb +++ b/app/controllers/public/api/v1/portals_controller.rb @@ -1,5 +1,6 @@ class Public::Api::V1::PortalsController < PublicController before_action :ensure_custom_domain_request, only: [:show] + before_action :redirect_to_portal_with_locale, only: [:show] before_action :portal layout 'portal' @@ -9,5 +10,12 @@ class Public::Api::V1::PortalsController < PublicController def portal @portal ||= Portal.find_by!(slug: params[:slug], archived: false) + @locale = params[:locale] || @portal.default_locale + end + + def redirect_to_portal_with_locale + return if params[:locale].present? + + redirect_to "/hc/#{@portal.slug}/#{@portal.default_locale}" end end diff --git a/app/views/public/api/v1/portals/show.html.erb b/app/views/public/api/v1/portals/show.html.erb index b6a8bc4a1..c46a286d9 100644 --- a/app/views/public/api/v1/portals/show.html.erb +++ b/app/views/public/api/v1/portals/show.html.erb @@ -1,7 +1,7 @@ <%= render "public/api/v1/portals/hero", portal: @portal %>
- <% @portal.categories.where(locale: params[:locale]).each do |category| %> + <% @portal.categories.where(locale: @locale).each do |category| %> <%= render "public/api/v1/portals/category-block", category: category, portal: @portal %> <% end %>
diff --git a/config/routes.rb b/config/routes.rb index 681227ef6..903c5efc8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -287,6 +287,7 @@ Rails.application.routes.draw do end end + get 'hc/:slug', to: 'public/api/v1/portals#show' get 'hc/:slug/:locale', to: 'public/api/v1/portals#show' get 'hc/:slug/:locale/categories', to: 'public/api/v1/portals/categories#index' get 'hc/:slug/:locale/:category_slug', to: 'public/api/v1/portals/categories#show'