byCoder
byCoder

Reputation: 9184

Rails generate in namespace one more namespace, there resource and there one more resource: subfolder prblem

I can't understand how can i generate resource in namespaces, and resource in resource: have problems with subfoldering... How could i generate in rails controller's and views for such route:

  namespace :admin do
    namespace :catalogs do  
      namespace :parfume do  
        resource :brand do  
          resource :models do  

i try so:

rails g scaffold Admin::Catalog

etc...

rails g scaffold Admin::Catalog::To::Brand

but in route i see that rails is generating many lines of route of such kind:

  namespace :catalogs do  
      namespace :to do  
        namespace :brand do  
          namespace :models do  
            namespace :types do 
              resources :to_articles 
            end 
          end 
        end 
      end 
    end
    namespace :catalogs do  
      namespace :to do  
        namespace :brand do  
          namespace :models do 
            resources :types 
          end 
        end 
      end 
    end

    namespace :catalogs do  
      namespace :to do  
        namespace :brand do 
          resources :models 
        end 
      end 
    end

but i want that it must be done via namespaces and resources, not so how now rails generators do...

So which command will be good for generating resource in subfolder one time as namespace, other as resourse?, and am i on right way?

Note: it must be so long path!

Upvotes: 0

Views: 874

Answers (1)

Bachan Smruty
Bachan Smruty

Reputation: 5734

Please have a try with

rails g scaffold Admin/Catalog/Parfume/Brand

It will create the routes as following

namespace :admin do
    namespace :catalog do
      namespace :parfume do
        resources :brands
      end
    end
  end

You can do it manually. Lets you have admin and catalog namespace. In side the catalog namespace you have parfume and brands as controllers. And suppose users is a controller in the admin namespace.

So through the following code, you will create the parfume and brands controllers.

rails g controller admin/catalog/parfume
rails g controller admin/catalog/brands

This will create the controllers in your application. But in the routes file you need to add the code manually as following

namespace :admin do
    namespace :catalog do
      resources :parfume
      resources :brand
    end
  end

But if you have brands as the nested resources of parfume controller, then you need to change the routes little bit as following

namespace :admin do
    namespace :catalog do
      resources :parfume do
        resources :brand
      end
    end
  end

And then, as per our example, we have an users controller in the admin namespace. So we need to create an users controller in the admin namespace, Following is the code to create users controller

rails g controller admin/users

And in addition to it, we need to add a line of code in our routes file as well.

  namespace :admin do
    namespace :catalog do
      resources :parfume do
        resources :brand
      end
    end

    resources :users
  end

Scaffolding creates the controller and model but as per your requirement, it is creating the extra code in routes. SO we can optimize it as per the above example.

And regarding creating model, you can create it by the following command

rails g model YourModelName

Upvotes: 1

Related Questions