Ruby on rails can';t使用url_从嵌套路由名称空间的两个级别中退出
在我的Rails 3.0.10应用程序中,我有3层嵌套路由。像这样:Ruby on rails can';t使用url_从嵌套路由名称空间的两个级别中退出,ruby-on-rails,ruby-on-rails-3,routing,Ruby On Rails,Ruby On Rails 3,Routing,在我的Rails 3.0.10应用程序中,我有3层嵌套路由。像这样: resources :foo namespace :bar do resources :foo namespace :baz do resources :foo end end 在控制器中时,foo或bar/foo似乎都可以正常工作,但一旦进入控制器bar/baz/foo,我就无法获得到第一级控制器的路由 示例代码: 应用程序_controller.rb: class ApplicationControl
resources :foo
namespace :bar do
resources :foo
namespace :baz do
resources :foo
end
end
在控制器中时,foo或bar/foo似乎都可以正常工作,但一旦进入控制器bar/baz/foo,我就无法获得到第一级控制器的路由
示例代码:
应用程序_controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery
def urls_dump
[
{:controller => "foo"},
{:controller => "/foo"},
].map do |opts|
begin
url_for(opts.dup)
rescue => e
"BAD: #{opts.inspect}: #{e}"
end
end.join("\n") + "\n"
end
end
class FooController < ApplicationController
def index
render :text => urls_dump
end
end
module Bar
class FooController < ApplicationController
def index
render :text => urls_dump
end
end
end
module Bar
module Baz
class FooController < ApplicationController
def index
render :text => urls_dump
end
end
end
end
首先,我会认真重新考虑你的应用程序结构。两个名称空间似乎不必要。其次,我要检查一下你们的路线顺序。可能存在一些优先权问题。
✗ curl http://localhost:3000/foo
http://localhost:3000/foo
http://localhost:3000/foo
✗ curl http://localhost:3000/bar/foo
http://localhost:3000/bar/foo
http://localhost:3000/foo
✗ curl http://localhost:3000/bar/baz/foo
http://localhost:3000/bar/baz/foo
BAD: {:controller=>"/foo"}: No route matches {:controller=>"bar//foo"}