2014-09-25 3 views
0

레일 4.1.5의 before_action에 문제가 있습니다. 새 응용 프로그램에서 완전히 무시 된 것처럼 보입니다. http://guides.rubyonrails.org/i18n.htmlBefore_action rails ignored 4.1.5

응용 프로그램 컨트롤러 :

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 

    # Set current language to user params[:locale] if exists otherwise, the default_locale is used(en) 
    before_action :set_locale 

    def set_locale 
    I18n.locale = params[:locale] || I18n.default_locale 
    end 

    def default_url_options(options={}) 
    logger.debug "default_url_options is passed options: #{options.inspect}\n" 
    { locale: I18n.locale } 
    end 

end 

내 '큰'컨트롤러 :

class WelcomeController < ActionController::Base 
    def helloworld 
    end 
end 

내 경로 내 응용 프로그램의 국제화를 사용하기 위해 노력하고있어

, 난 설명서를 따라 .rb :

Rails.application.routes.draw do 
    scope "/:locale" do 
    get '/' => 'welcome#helloworld' 
    end 
end 

en.yml :

en: 
    hello: "Hello world !" 

fr.yml : 나는 그것이 작동하는 그런 내 컨트롤러에 직접 I18n.locale을 변경하려고했습니다

fr: 
    hello: "Bonjour le monde !" 

...

class WelcomeController < ActionController::Base 
    def helloworld 
    I18n.locale = :fr 
    end 
end 

그 이유는 내 before_action 무시됩니다 생각하지만 왜?

답변

1

두 경우 모두 ActionController::Base에서 상속 받기 때문입니다. 이전에 모든 컨트롤러에서 이전 액션을 상속 받으려는 경우 ApplicationController

class WelcomeController < ApplicationController 
    def helloworld 
    end 
end 
+0

감사합니다. 정말 바보예요. :) – Ayoros

관련 문제