首页 > rails路由中的namespace

rails路由中的namespace

我的route.rb文件中有:

namespace :admin do
    resources :products, :brands, :categories, :effects
end

rake routes后,生成的路由如下:

           Prefix Verb   URI Pattern                          Controller#Action
               root GET    /                                    page#index
           products GET    /products(.:format)                  products#index
            product GET    /products/:id(.:format)              products#show
             brands GET    /brands(.:format)                    brands#index
         categories GET    /categories(.:format)                categories#index
            effects GET    /effects(.:format)                   effects#index
     admin_products GET    /admin/products(.:format)            admin/products#index
                    POST   /admin/products(.:format)            admin/products#create
  new_admin_product GET    /admin/products/new(.:format)        admin/products#new
 edit_admin_product GET    /admin/products/:id/edit(.:format)   admin/products#edit
      admin_product GET    /admin/products/:id(.:format)        admin/products#show
                    PATCH  /admin/products/:id(.:format)        admin/products#update
                    PUT    /admin/products/:id(.:format)        admin/products#update
                    DELETE /admin/products/:id(.:format)        admin/products#destroy
       admin_brands GET    /admin/brands(.:format)              admin/brands#index
                    POST   /admin/brands(.:format)              admin/brands#create
    new_admin_brand GET    /admin/brands/new(.:format)          admin/brands#new
   edit_admin_brand GET    /admin/brands/:id/edit(.:format)     admin/brands#edit
        admin_brand GET    /admin/brands/:id(.:format)          admin/brands#show
                    PATCH  /admin/brands/:id(.:format)          admin/brands#update
                    PUT    /admin/brands/:id(.:format)          admin/brands#update
                    DELETE /admin/brands/:id(.:format)          admin/brands#destroy
   admin_categories GET    /admin/categories(.:format)          admin/categories#index
                    POST   /admin/categories(.:format)          admin/categories#create
 new_admin_category GET    /admin/categories/new(.:format)      admin/categories#new
edit_admin_category GET    /admin/categories/:id/edit(.:format) admin/categories#edit
     admin_category GET    /admin/categories/:id(.:format)      admin/categories#show
                    PATCH  /admin/categories/:id(.:format)      admin/categories#update
                    PUT    /admin/categories/:id(.:format)      admin/categories#update
                    DELETE /admin/categories/:id(.:format)      admin/categories#destroy
      admin_effects GET    /admin/effects(.:format)             admin/effects#index
                    POST   /admin/effects(.:format)             admin/effects#create
   new_admin_effect GET    /admin/effects/new(.:format)         admin/effects#new
  edit_admin_effect GET    /admin/effects/:id/edit(.:format)    admin/effects#edit
       admin_effect GET    /admin/effects/:id(.:format)         admin/effects#show
                    PATCH  /admin/effects/:id(.:format)         admin/effects#update
                    PUT    /admin/effects/:id(.:format)         admin/effects#update
                    DELETE /admin/effects/:id(.:format)         admin/effects#destroy
              admin GET    /admin(.:format)                     admin#index

我想知道,类似admin/brands#new这样的控制器和动作,应该怎么生成?


如楼主所说,其实是这样的:
controller里面有一个文件夹,叫做“admin”,这个文件夹里面有一些文件:
products_controller.rb
brands_controller.rb
等等


参考这个: http://guides.ruby-china.org/routing.html#%E6%8E%A7%E5%88%B6%E5%99%A8%...

app/controllers/admin/brands_controller.rb

class Admin::BrandsController < ApplicationController
...

routes.rb

namespace :admin do
  resources :brands
end

楼上正解,另外可以试试

shgenerate scaffold admin::blog

参见Rails Guide

【热门文章】
【热门文章】