-1

로케일을 설정하고 응용 프로그램 컨트롤러에서 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 

답변

1

는 당신은

class ApplicationController < ActionController::Base 

    catrr_accesor :locale_set 
    before_action :set_locale :if => lambda {|c| locale_set} 


    def set_locale 
    I18n.locale = current_user.locale if current_user 
    ApplicationController.locale_set = true 
    end 
end 

를 사용해야합니다.

def set_locale 
    I18n.locale = user_signed_in? ? current_user.locale.to_sym : (params[:local] || I18n.default_locale) 
    end 

당신이 그런 일에 끝낼 수 있습니다 유증으로 스윙하기 : 나는 before_filter/before_action 방법을 피하기 위해 노력하고있어

# get locale of user 
    def after_sign_in_path_for(resource_or_scope) 
    if resource_or_scope.is_a?(User) && resource_or_scope.locale != I18n.locale 
     I18n.locale = resource_or_scope.locale.to_sym || I18n.default_locale 
    end 
    session[:previous_url] || root_path 
    end 
+0

그리고 어쩌면 당신이 그런 짓을하고 싶습니다. –

+0

어쩌면 한 번만 실행해야하는 필터 전에 조건부로 시도해보십시오. 아직도 당신이 찾고있는 것이 아니라면, 사용한다면 몇 가지 갈고리를 고쳐야할까요? –