Search code examples
rubyruby-on-rails-5.2

Routing in rails 5.2


I have a RoR app with an admin_controller, the admin is able to CRUD Item, now I am a bit confused when it comes to routing. How can I create the app in a way that I can have a link to new_item be like: admin/item/new instead of creating an item controller with a authenticate_admin? method (since I use Device)

All in all how is this kind of routing achieved in rails

e.g. admin/orders/:id or store/sales/lates


Solution

  • Ideally , your admin_controller shouldn't do any CURD actions on an Item object. You should use an items_controller for that.

    to achieve what you are trying to do, you can use an admin name space and maybe get the item_controller to be inherited from the admin controller.

    So, you can have something like,

    #app/controllers
    AdminController < ApplicationController
    
    end
    
    #app/controllers/admin/
    module Admin
      ItemsController < AdminController
    
      end
    end
    
    #config/routes
    namespace :admin do
      resources :items
    end
    

    rails namespaces