
Originally Posted by
Skyblaze
so you'are saying that in general i have to create a controller for every entity i have to manage and instead of using "admin" and "customers" controllers i only use entities controllers and protect any actions for admin users right?
Yeah basically. Here's a quick example: let's say you have two models - User and Post. Posts belong to users and Users have many posts. You want users who aren't logged in to be able to view posts, but not edit/create/destroy them. Here's what the Post controller would look like:
Code:
class PostsController < ApplicationController
before_filter :login_required, :except => [:index, :show]
def index
....
end
#other actions
end
Anyone who tries to create a new post will have to log in first. If you wanted to, you could even show a different layout based on which action is being displayed, so you can have an admin layout and a regular user layout in the same controller:
Code:
class PostsController < ApplicationController
before_filter :login_required, :except => [:index, :show]
layout :user_layout
def index
....
end
#other actions
private
def user_layout
(%w(index show).include? self.action_name) ? 'main' : 'admin'
end
end

Originally Posted by
Skyblaze
And for other actions/template not directly connected to those entities i must create another more generic controller?
I'm not sure what you mean by this. Care to expand a little?
Bookmarks