Rails 3 - 具有范围资源的 URL 助手

发布于 2024-12-06 04:29:39 字数 1517 浏览 0 评论 0原文

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

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

Using '>; rake 路线' 我得到了标准路线,包括:

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?有什么解决方法的想法,以便我仍然可以使用无前缀的路由吗?

I have a scoped resource in my routes file:

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

Using '> rake routes' I get the standard routes, including:

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

However when I use the console (and this is what's failing in my tests) to get the url for instances of Mymodel I get errors:

> 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>'

This might be a gotcha, but Mymodel is also a subclass using the standard Rails single table inheritance.

Any thoughts? Why is it looking for physical_mymodel_url instead of mymodel_url? Any ideas for workarounds so that I can still use the unprefixed routes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2024-12-13 04:29:39

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

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

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

namespace :physical do
  resources :mymodel
end

You're only using the scope to tell it the module where the controller can be found in. If you want to use the physical prefix on your routes then you could do this:

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

Alternatively, you could just use the namespace method which will do the same thing:

namespace :physical do
  resources :mymodel
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文