views:

64

answers:

2

Hi folks,

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.

A: 

What you're describing sounds like a good example of a Cross-cutting concern. Specifically, user authentication is a concern which affects many other concerns. In your case, user rights affects the posting of articles, but in the future it's reasonable to assume it could affect things like control of comments, addition of categories, etc.

In cases like this, it's best to keep logic in the controllers that address the primary concern (i.e. creating an article would fall under the article controller), and offload authentication and role security onto an additional module that your controllers can all use. This allows you to keep article code centralized in a single controller, and also allows you to add role-level security in other controllers in the future without significant refactoring.

There's a variety of plugins for authentication and role-level security in Rails. I'd do a little research and find one that suits your needs best.

Ryan Brunner
I just implemented authlogic and its awesome and I know about before filters you can setup but I think its a not really secure set it up like that. Am I wrong?
OemerA
+1  A: 

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):

# routes.rb
resources :users
resources :posts
resources :pages

namespace :admin do |admin|
  match '/' => 'dashboard#index'
  resources :users
  resources :posts
  resources :pages
end

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:

class Admin::BaseController < ApplicationController
  before_filter :require_user
  layout 'admin'
end

Now make a directory in app/controllers called "admin". Make controllers in there as normal, but have them inherit from your base controller:

# pages_controller.rb
class Admin::PagesController < Admin::BaseController
  # Controller code in here
end

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!

trevrosen
And to answer your earlier question, yes before_filters are the way that an auth module (like AuthLogic) honors or denies requests sent to particular routes. Assuming the rest of it is installed correctly, it is proper/normal to use before_filter macros in your controllers to implement the actual gatekeeping.
trevrosen
Thank you! That helped me a lot to understand those separations.I will try that out and I will let you know about how it is going.
OemerA