Ruby on rails 在将模块映射到命名空间的根时,避免在helper方法中使用双下划线

Ruby on rails 在将模块映射到命名空间的根时,避免在helper方法中使用双下划线,ruby-on-rails,Ruby On Rails,我有一个命名空间Foo(目录/app/controllers/Foo)和一个控制器Foo::BarController(目录/app/controllers/Foo/bar\u controller.rb) 我想将Foo::BarController的资源路由到Foo的根目录。我在路线上试过: Rails.application.routes.draw do namespace :foo do scope module: :bar do resources ""

我有一个命名空间
Foo
(目录
/app/controllers/Foo
)和一个控制器
Foo::BarController
(目录
/app/controllers/Foo/bar\u controller.rb

我想将
Foo::BarController
的资源路由到
Foo
的根目录。我在路线上试过:

Rails.application.routes.draw do
  namespace :foo do
    scope module: :bar do
      resources ""
    end
  end
end
这提供了我想要的路径和控制器方法之间的映射。但是,对于
GET/foo(:format)
的helper方法,我得到了

foo__index_path
我想去掉多余的下划线,以便

foo_index_path
我怎么做

由于名称空间
foo
中有其他控制器,如果可能的话,我想用
名称空间:foo do围绕整个路由规范。。。结束
如上所述的阻塞,并避免将根案例作为特殊案例处理



Rails生成带有连续下划线的helper方法的这种行为是有意的,还是我可以称之为bug?

双下划线是因为资源名称通常会出现在那里。由于您的资源名为
,因此您最终将使用
foo\uu index
作为路由帮助器

相反,您可能希望将路由文件更改为

scope :foo, module: :foo do
  resources :bar, path: "", as: :foo
  resources :baz # just to show what happens to the other routes
end
然后
rake routes
为您提供

   Prefix Verb   URI Pattern                 Controller#Action
foo_index GET    /foo(.:format)              foo/bar#index
          POST   /foo(.:format)              foo/bar#create
  new_foo GET    /foo/new(.:format)          foo/bar#new
 edit_foo GET    /foo/:id/edit(.:format)     foo/bar#edit
      foo GET    /foo/:id(.:format)          foo/bar#show
          PATCH  /foo/:id(.:format)          foo/bar#update
          PUT    /foo/:id(.:format)          foo/bar#update
          DELETE /foo/:id(.:format)          foo/bar#destroy
baz_index GET    /foo/baz(.:format)          foo/baz#index
          POST   /foo/baz(.:format)          foo/baz#create
  new_baz GET    /foo/baz/new(.:format)      foo/baz#new
 edit_baz GET    /foo/baz/:id/edit(.:format) foo/baz#edit
      baz GET    /foo/baz/:id(.:format)      foo/baz#show
          PATCH  /foo/baz/:id(.:format)      foo/baz#update
          PUT    /foo/baz/:id(.:format)      foo/baz#update
          DELETE /foo/baz/:id(.:format)      foo/baz#destroy
而不是

    Prefix Verb   URI Pattern             Controller#Action
foo__index GET    /foo(.:format)          foo/bar/#index
           POST   /foo(.:format)          foo/bar/#create
   new_foo GET    /foo/new(.:format)      foo/bar/#new
  edit_foo GET    /foo/:id/edit(.:format) foo/bar/#edit
       foo GET    /foo/:id(.:format)      foo/bar/#show
           PATCH  /foo/:id(.:format)      foo/bar/#update
           PUT    /foo/:id(.:format)      foo/bar/#update
           DELETE /foo/:id(.:format)      foo/bar/#destroy
对于您当前的设置,我相信这会引发某种错误(我使用范围的初始设置获得未初始化常量
Foo::Bar

然后,如果希望嵌套在
Foo
命名空间中的其他资源保留
Foo_baz_索引而不是
baz_索引
,则可以将它们放在另一个范围中

scope :foo, module: :foo do
  resources :bar, path: "/", as: :foo

  scope as: :foo do
    resources :baz
  end
end
产生

       Prefix Verb   URI Pattern                 Controller#Action
    foo_index GET    /foo(.:format)              foo/bar#index
              POST   /foo(.:format)              foo/bar#create
      new_foo GET    /foo/new(.:format)          foo/bar#new
     edit_foo GET    /foo/:id/edit(.:format)     foo/bar#edit
          foo GET    /foo/:id(.:format)          foo/bar#show
              PATCH  /foo/:id(.:format)          foo/bar#update
              PUT    /foo/:id(.:format)          foo/bar#update
              DELETE /foo/:id(.:format)          foo/bar#destroy
foo_baz_index GET    /foo/baz(.:format)          foo/baz#index
              POST   /foo/baz(.:format)          foo/baz#create
  new_foo_baz GET    /foo/baz/new(.:format)      foo/baz#new
 edit_foo_baz GET    /foo/baz/:id/edit(.:format) foo/baz#edit
      foo_baz GET    /foo/baz/:id(.:format)      foo/baz#show
              PATCH  /foo/baz/:id(.:format)      foo/baz#update
              PUT    /foo/baz/:id(.:format)      foo/baz#update
              DELETE /foo/baz/:id(.:format)      foo/baz#destroy

对不起,前后不一致。我会修复它。但是,我会得到
foo\u bar\u index\u path
,而我想要
foo\u index\u path