We add new method set_user_locale to render the page with the user's preferred locale. Note that we add a condition 'if params[:locale].blank?' to recover the user's preferred locale. This is necessary because it may be the case that the user does not have an associated locale, and when execute '@user.locale' when this value is 'nil', by default returns the default locale. As we do not want this to happen and we want the locale we receive as parameter to prevail in this case.
39 lines
1021 B
Ruby
39 lines
1021 B
Ruby
class SubscriptionsController < ApplicationController
|
|
before_action :set_user, :set_user_locale
|
|
skip_authorization_check
|
|
|
|
def edit
|
|
end
|
|
|
|
def update
|
|
@user.update!(subscriptions_params)
|
|
redirect_to edit_subscriptions_path(token: @user.subscriptions_token),
|
|
notice: t("flash.actions.save_changes.notice")
|
|
end
|
|
|
|
private
|
|
|
|
def set_user
|
|
@user = if params[:token].present?
|
|
User.find_by!(subscriptions_token: params[:token])
|
|
else
|
|
current_user || raise(CanCan::AccessDenied)
|
|
end
|
|
end
|
|
|
|
def subscriptions_params
|
|
params.require(:user).permit(allowed_params)
|
|
end
|
|
|
|
def allowed_params
|
|
[:email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter]
|
|
end
|
|
|
|
def set_user_locale
|
|
if params[:locale].blank?
|
|
I18n.locale = I18n.available_locales.find { |locale| locale == @user.locale&.to_sym }
|
|
session[:locale] = I18n.locale
|
|
end
|
|
end
|
|
end
|