Skip to content

Commit 270d516

Browse files
author
Rens van Moorsel
committed
chore: cleaner way of getting parameter
1 parent c7068e4 commit 270d516

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

app/controllers/public/home_controller.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ def index
1515

1616
def redirect_to_locale
1717
# Check if the URL already contains a locale
18-
return if request.path.match?(/^\/\?l=(nl|en)(\/|$)/)
18+
return if params[:l].in?(%w[nl en])
1919

2020
# Get the language from the Accept-Language header
2121
locale = request.env['HTTP_ACCEPT_LANGUAGE'].to_s.start_with?('nl') ? 'nl' : 'en'
22-
22+
2323
# Redirect to /?l=nl/... or /?l=en/...
24-
redirect_to("/?l=#{ locale }#{ request.path }", allow_other_host: false)
24+
new_params = request.query_parameters.merge(l: locale)
25+
redirect_to url_for(params: new_params.merge(only_path: true)), allow_other_host: false
2526
end
2627

2728
def create
@@ -36,11 +37,7 @@ def create
3637
if flash[:error].nil? && @member.save
3738
# create account and send welcome email
3839
user = User.create_on_member_enrollment!(@member)
39-
if request.path.start_with?('/?l=nl')
40-
user.language = 0
41-
elsif request.path.start_with?('/?l=en')
42-
user.language = 1
43-
end
40+
user.language = { 'nl' => 0, 'en' => 1 }[params[:l]] if params[:l].present?
4441
user.save
4542
user.resend_confirmation!(:activation_instructions)
4643

0 commit comments

Comments
 (0)