ruby-on-rails - 为命名空间下的命名路由助手添加前缀

标签 ruby-on-rails rails-routing

这就是通用命名空间的样子。

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/

相关文章:

ruby-on-rails - redirect_to 在救援 block 中的行为

ruby-on-rails - 无法在 Rails 中创建具有带前导点的段的路由(以验证 Let's Encrypt)

ruby-on-rails - 奇怪的 Rails 资源路由行为

ruby-on-rails - 我在 ActionController 中遇到路由错误,直到重新启动 rails 服务器

ruby-on-rails-3 - 如何解决这些冲突的 Rails 路由?

html - 当 anchor 标签内部有跨度时, anchor 标签不起作用

ruby-on-rails - 在模型中使用 Rails 助手

ruby-on-rails - gem 服务器: How to update gems with missing rdoc?

ruby-on-rails - 如何运行Rails服务器守护程序?

ruby-on-rails - 你如何在 Rails 4 中实现虚拟 link_to