2017-09-12 11 views
0

、私はフィルタ以前のようにロケール処理を強制しています:なぜデフォルトロケールが機能しないのですか?自分のアプリケーションのコントローラで

before_filter :set_locale 

def set_locale 
    #I18n.default_locale is en 
    I18n.locale = extract_locale_from_tld || I18n.default_locale 
end 

def extract_locale_from_tld 
    parsed_locale = params[:locale] || ((lang = request.env['HTTP_ACCEPT_LANGUAGE']) && lang[/^[a-z]{2}/]) 
    #so, english is the default language. 
    parsed_locale= 'en' if parsed_locale.nil? 
    I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil 
end 

しかし、私はhttp://localhost:3000/ko/labを訪問しようとした場合、それは最終的にエラー404あるルーティングエラーになります。 enter image description here

何かアドバイス:

ルーティングエラーになり、次の突き止めのうち任意のロケール

# -*- encoding : utf-8 -*- 
MyApp::Application.routes.draw do 

    mount Alchemy::Engine => 'blog' 

    Rails.application.routes.draw do 
    filter :pagination, :uuid, :locale 
    end 

    devise_for :users, :controllers => { :omniauth_callbacks => "callbacks", 
             :sessions => "users/sessions", 
             :registrations => "users/registrations"} 



    resources :authentications 


    resources :experiments 
    get 'experiments/:id/info_edit' => 'experiments#info_edit',:constraints => { :id => /\d+/ } 

    get 'sitemap.xml', :to => 'sitemap#index', :defaults => {:format => 'xml'} 



    match 'lang' => 'home#set_lang', :via => [:get] 

    #match 'free_trial' => 'home#free_trial', :via => [:get] 
    match 'useful_links' => 'home#useful_links', :via => [:get] 
    match 'home' => 'home#home', :via => [:get] 


    match 'contact-us' => 'contact#new', :as => 'contact_us', :via => :get 
    match 'contact' => 'contact#create', :as => 'contact', :via => :post 

    match 'dashboard' => 'experiments#index', :via => [:get] 
    post 'notifications' => 'subscriptions#instant_payment_notification' 


    match 'users_experiments' => 'experiments#users_experiments', :via => [:get] 
    match 'hire_us' => 'home#hire_us', :via => [:get] 


    get 'lab' => 'experiments#lab' 

    get 'experiments/:id/review-edit' => 'experiments#review', :as => :review 
    get 'experiments/:id/cancel-edit' => 'experiments#cancel_edit', :as => :cancel_edit 

    get 'experiments/:id/approve-edit' => 'experiments#approve', :as => :approve 
    get 'experiments/:id/reject-edit' => 'experiments#reject', :as => :reject 


    get 'experiments/:id/profile' => 'experiments#profile', :as => :profile 



    match 'amazon' => 'experiments#amazon', :via => [:get] 


    match 'trial_account' => 'home#trial_account', :via => [:get] 
    match 'student_account' => 'home#student_account', :via => [:get] 
    match 'school_account' => 'home#school_account', :via => [:get] 



    match 'create_account' => 'home#registration_redirect', :via => [:get] 
    match 'users/create_account' => 'home#registration_entrance', :via => [:get] 
    match 'registration_redirect' => 'home#registration_redirect', :via=>[:post] 
    # You can have the root of your site routed with "root" 
    # just remember to delete public/index.html. 
    root :to => 'home#home' 

    get "/auth/oauth2/callback" => "auth0#callback" 
    get "/auth/verification_complete" => "auth0#verification_complete" 
    get "/auth/failure" => "auth0#failure" 



end 

私のサーバーのログ:マイroute.er

EDIT

Started GET "/ko/lab" for 127.0.0.1 at 2017-09-12 08:58:56 +0300 Processing by ApplicationController#routing_error as HTML
Parameters: {"path"=>"ko/lab"} User Load (202.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 12899]] Completed 404 Not Found in 205ms ko/lab excluded from capture: DSN not set

ActionController::RoutingError (ko/lab):
app/controllers/application_controller.rb:35:in routing_error'
lib/rack/seoredirect.rb:20:in
call'

+0

あなたが表示され、ログを含めることができます参照してください。ルーティングエラーとroutes.rbファイル? – max

答えて

0

デフォルトはローカルの作業を行いますが、あなたが/:locale/labと一致するルートを持っていません。それは簡単です。

scopeを使用してローカライズされたルートを作成することはできますが、Railsではこれを行いません。

scope '(:locale)', locale: /#{I18n.available_locales.join('|')}/ do 
    get '/' => 'home#home' # this will map /en to home#home 
    get 'lab' => 'experiments#lab' 
    # ... 
end 

これはおそらく、その最大限にリソースヘルパーを使用して、本当に厄介なroutesファイルをリファクタリングするには良い時間です:

resources :experiments do 
    member do 
    get :info_edit 
    # ... 
    end 
end 

http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

+0

あなたが提案したことを試したが、引き続きルーティングエラーが発生する – simo

関連する問題