Ruby on rails 3 使用Rails 3根路径和根路径与区域设置url路径

Ruby on rails 3 使用Rails 3根路径和根路径与区域设置url路径,ruby-on-rails-3,routing,Ruby On Rails 3,Routing,我有一个简单的路线和一个简单的主菜单: routes.rb scope "(:locale)", :locale => /en|nl/ do resources :posts, :only => [:show, :index] end root :to => "posts#index" 菜单 menu\u link\u to是一个围绕link\u to的简单包装器,在选择当前菜单时添加了设置“活动”类的功能: def menu_link_to(title, optio

我有一个简单的路线和一个简单的主菜单:

routes.rb

scope "(:locale)", :locale => /en|nl/ do
  resources :posts, :only => [:show, :index]
end
root :to => "posts#index"
菜单

menu\u link\u to
是一个围绕
link\u to
的简单包装器,在选择当前菜单时添加了设置“活动”类的功能:

  def menu_link_to(title, options = {}, html_options = {})
    if current_page?(options)
      html_options[:class] ||= []
      html_options[:class] << "active"
    end

    link_to(title, options, html_options)
  end
def菜单链接(标题,选项={},html选项={})
如果是当前页面?(选项)
html|U选项[:类]| |=[]
html_选项[:类]/
  • 博客->/
  • 关于等等
  • NL->/NL/posts
    • Blog->/nl/posts。在菜单中,这是与英语相同的条目,只是具有不同的区域设置前缀
  • 我真的不介意EN和EN»博客链接到/EN/posts,如果这样可以简化内容的话

    但是现在,当访问根路径时,current_path()将返回FALSE。它的行为也很奇怪,因为rails会尝试将locale设置为?name=value参数:
    /?locale=en

    我想我遗漏了一些重要的wrt根路径或路由。有什么想法吗


    编辑:澄清“博客”项。

    您是否尝试在routes.rb文件中添加匹配项?在根目录之前的右键:

    match'/:locale'=>“仪表板#索引”

    这是在

    或者您也可以将根目录放入范围,如下所示:

    scope "(:locale)", :locale => /en|nl/ do
      resources :posts, :only => [:show, :index]
      root :to => "posts#index"
    end
    
    然后,如果不指定区域设置,将使用默认区域设置

    scope "(:locale)", :locale => /en|nl/ do
      resources :posts, :only => [:show, :index]
      root :to => "posts#index"
    end