I have a problem with the understanding MVC architecture.
It’s not that I don’t know anything about MVC. Everything makes sense to me in a MVC architecture but if I want to start to develop my app in an MVC architecture I’m stuck.
Basically there are a lot ways to do what you want in the programming world but I want to do it like it was supposed to be.
So maybe there is someone who can help me out.
But here my recent problem with MVC:
I want to write my own blog in Ruby on Rails. This not a big deal I think.
I would have my models like articles, comments, user and much more. For each of them I would create a controller to manage them and all.
The problem is when it comes to the Admin-Panel. I want that an article can only created in the Admin-Panel.
So what should I do?
Should I create a Admin-Panel controller to manage all those tasks which can only accomplished in the Admin-Panel at all?
Otherwise I think it is too much for a single controller.
I want that my urls looks something like this:
For Admin-Panel tasks: example.com/admin/article/create
For Viewers: example.com/article/show
(I think restful Rails routes are looking different but I think you get what I want)
How would you accomplish this task in an MVC architecture and how should it be done?
Can you help to understand those MVC tasks much better?
Thank you in advance.
The two things to keep in mind when making an admin area are
1) you can create namespaces for routes to get the /admin URLs you’re looking for and
2) you can have controllers inherit from other descendants of ActionController
So to make an admin area, you’d want to have RESTful resources declared in a namespace (assumes Rails 3 routes):
The top set is the public ones and the bottom set gives you the admin routes like /admin/users/new and /admin/posts/1, etc. I’m also assuming you might want a “dashboard” so I’m setting up a route to the index method of an Admin::DashboardController
Then you create an admin base controller that descends from ApplicationController. Use it to hold your admin area layout and your authentication filters:
Now make a directory in app/controllers called “admin”. Make controllers in there as normal, but have them inherit from your base controller:
Make a corresponding directory in app/views for “admin” and you’re good to go — everything is namespaced out and views/controllers would behave like you think.
You can always run “rake routes” to see all the admin routes.
Hope that helps!