2

アプリに別の言語を追加しました。したがって、静的ページには次のルートを使用しています。

scope "(:locale)", locale: /en|br/ do
  get "static_pages/about"
  match '/about', to: 'static_pages#about'
  ...
end

それは正常に動作しており、結果は次のとおりです。

http://localhost:3000/en/about

ただし、言語を切り替えるたびに、一致する代わりにフル パスが返されます。

http://localhost:3000/en/static_pages/about

私が言語を切り替える方法:

#links
<%= link_to (image_tag '/england.png'), url_for( locale: 'en' ) %>
<%= link_to (image_tag '/brazil.png'), url_for( locale: 'br' ) %>  

#application controller
before_filter :set_locale
def set_locale
  I18n.locale = params[:locale]
end

def default_url_options(options={})
  { locale: I18n.locale }
end

CSSファイルで現在のパスを使用しているため、言語を切り替えるたびにレイアウトが台無しになるため、これは問題です。

<%= link_to (t 'nav.about'), about_path, class: current_p(about_path) %> 

#helper
def current_p(path)
   "current" if current_page?(path)
end

match言語を切り替えるときにルートを返す方法を見つけようとしています。何か案が?

4

1 に答える 1

1

I have solved the problem combining match and get.
So, instead of:

scope "(:locale)", locale: /en|br/ do
  get "static_pages/about"
  match '/about', to: 'static_pages#about'
  ...
end

I have now:

scope "(:locale)", locale: /en|br/ do
  match '/about', to: 'static_pages#about', via: 'get'
  ...
end

EDIT - Thanks to sevenseacat, an easier and shorter solution:

scope "(:locale)", locale: /en|br/ do
  get '/about' => 'static_pages#about'
  ...
end
于 2013-02-07T14:32:28.693 に答える