使用范围区域设置路由路由

时间:2013-01-31 11:57:21

标签: ruby-on-rails ruby-on-rails-3 routing locale

我在我的应用中添加了另一种语言。所以,我使用以下路由作为静态页面:

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路由的方法。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我解决了matchget相结合的问题 所以,而不是:

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

我现在有:

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

编辑 - 感谢sevenseacat,这是一个更简单,更简洁的解决方案:

scope "(:locale)", locale: /en|br/ do
  get '/about' => 'static_pages#about'
  ...
end
相关问题