Rails 3 - 具有范围资源的 URL 助手
我的路由文件中有一个范围资源:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仅使用范围来告诉它可以在其中找到控制器的模块。如果您想在路线上使用
physical
前缀,那么您可以这样做:或者,您可以只使用
namespace
方法将执行相同的操作: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:Alternatively, you could just use the
namespace
method which will do the same thing: