我有一个命名空间的模型和控制器:
# app/controllers/foo_stuff/bars_controller.rb
module FooStuff
class BarsController < ApplicationController
# ..
end
end
# app/models/foo_stuff/bar.rb
module FooStuff
class Bar < ApplicationRecord
# ...
end
end
我想:
- URL看起来像
/foo/1
,这将路由到FooStuff::BarController#show
. - 命名的路由帮助器看起来像
foo_url
,例如foo_url(@bar)
.
我的路由是这样设置的:
# config/routes.rb
scope module: 'foo_stuff', as: 'foo' do
resources :bars, path: 'foo'
end
当我运行rails routes
时,这看起来是正确的.
然而,当我try 的时候:
<%= link_to('Something', @bar) %>
...Rils抱怨undefined method 'foo_stuff_bar_url' for an instance of #<Class:...>
个.
如果我显式地使用命名路由,这是可以的:
<%= link_to('Something', foo_url(@bar) %>
我希望link_to()
能从路由中找出@bar
模型调用的路由助手. 但事实并非如此.
我是不是遗漏了什么?