有没有办法设置区域设置并在请求之间保持设置而不在应用程序控制器中使用 before_action/before_filter?
我试图避免我目前的解决方案:
class ApplicationController < ActionController::Base
before_action :set_locale
def set_locale
I18n.locale = current_user.locale if current_user
end
end
class LocaleController < ApplicationController
skip_authorization_check
def index
locale = params[:locale]
raise 'Unsupported locale' unless ['en', 'pt'].include?(locale)
error_message = "Could not set locale" unless current_user.update_column(:locale, locale)
I18n.locale = current_user.locale if error_message.nil?
redirect_to :back, :alert => error_message
end
end