0

我的路由文件中有一个范围资源:

scope :module => "physical" do
    resources :mymodels
end

使用 '> rake routes' 我得到标准路线,包括:

mymodel GET    /mymodels/:id(.:format)    {:action=>"show", :controller=>"physical/mymodels"}

但是,当我使用控制台(这是我的测试失败的原因)获取 Mymodel 实例的 url 时,我收到错误:

> m = Physical::Mymodel.new
> => {...model_attributes...}
> m.save
> => true
> app.url_for(m)
> NoMethodError: undefined method `physical_mymodel_url' for #<ActionDispatch::Integration::Session:0x00000105b15228>
from /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/testing/assertions/routing.rb:175:in `method_missing'
from /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/polymorphic_routes.rb:114:in `polymorphic_url'
from /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/url_for.rb:133:in `url_for'
from (irb):13
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

这可能是一个陷阱,但 Mymodel 也是使用标准 Rails 单表继承的子类。

有什么想法吗?为什么要查找physical_mymodel_url 而不是mymodel_url?任何解决方法的想法,以便我仍然可以使用不带前缀的路线?

4

1 回答 1

2

您只是使用范围来告诉它可以在其中找到控制器的模块。如果您想在physical路由上使用前缀,那么您可以这样做:

scope :module => "physical", :as => "physical" do
  resources :mymodel
end

或者,您可以只使用namespace执行相同操作的方法:

namespace :physical do
  resources :mymodel
end
于 2011-09-22T18:30:25.633 回答