routes.rb:

resources :pages scope '/admin' do resources :pages end 

We get two paths site.com/pages and site.com/admin/pages which lead to the same controller pages_controller.rb . How in this controller to recognize what site.com/admin/pages is site.com/admin/pages . Where is the scope value stored? Parse the path to the presence of admin I think not the best solution?

The point is to give different views depending on the URL. Perhaps there is a better way? Can it be better to create 2 controllers pages_controller and admin_pages_controller ?

    1 answer 1

    Find ... you can, but it is better to beep clearly in the parameters. I conducted this experiment:

     resources :pages, only: :index scope '/admin', defaults: { admin: true } do resources :pages, only: :index end 

    If the route coincides inside the scop, params[:admin] will be true , and if not, it will not exist at all (i.e., trying to get it will return nil ).

    Why? Because the route system, in fact, serves as a route to parameter converter . If the “adminstness” of the route is a parameter, mark it explicitly.

    But making a separate controller is a good idea : you can do a lot of actions on the entire controller : access control, preloading the data needed for admin panel ... after all, you have two different sets of views! Already this is clearly asking for a separate controller. Their common parts can be brought into the module.

    • Thank you, this is what you need! - user183216