这就是通用命名空间的样子。
namespace :admin do
resources :posts
end
它创建了一个像这样的命名路由;
new_admin_post_path
这是我的问题;如何向命名空间下的命名路由添加前缀(如本例中的“new”)?
假设我的路线定义喜欢这个;
namespace :admin do
get 'post/new' => 'posts#new', as: 'post'
end
它创建了一个命名路由,如;
admin_post_path
我想为这个命名路由添加"new"前缀,并使它看起来像
new_admin_post_path
我不想使用 resources
.
最佳答案
只需尝试 route 的代码。
namespace :admin, as: '' do
get '/post/new' => 'posts#new', as: 'new_admin_post'
end
如果您不想将 admin 命名空间设为 nil,那么您可以这样做。为此,您需要将该路由从命名空间中移除:路由中的 admin 块
namespace :admin do
# your other routes
end
get '/admin/post/new' => 'admin/posts#new', :as => 'new_admin_post'
关于ruby-on-rails - 为命名空间下的命名路由助手添加前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17343504/