Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple controllers with a single model

I'm setting up a directory application for which I need to have two separate interfaces for the same Users table. Basically, administrators use the Users controller and views to list, edit, and add users, while non-admins need a separate interface which lists users in a completely different manner. To do this, would I be able to just set up another controller with different views but which accesses the Users model?

Sorry if this is a simple question, but I've had a hard time finding how to do this.

like image 844
Eric K Avatar asked Apr 23 '10 16:04

Eric K


People also ask

Can one model have multiple controllers?

No, you use a single controller method to grab the data from both models.

Can two controllers have same view?

Yes, It is possible to share a view across multiple controllers by putting a view into the shared folder. By doing like this, you can automatically make the view available across multiple controllers.

Can we have multiple controllers in MVC?

Yes we can create multiple controllers in Spring MVC.

Should each model have a controller?

You need a controller just when you have request actions on the relative model; When a model is just for data calculation and basic business logic implementation, don't generate the controller.


1 Answers

Why not put the admin part into a separate namespace - you would have Admin::UsersController with views in app/views/admin/users/. And your users would go to UsersController with its own views in app/views/users/.

The routing is defined like this:

map.namespace :admin do |admin|
  admin.resources :users
end


map.resources :users

And can be got to via admin_users_path and users_path

like image 112
alex.zherdev Avatar answered Sep 29 '22 14:09

alex.zherdev