Architecture for various user types and account creation forms
I'm using Rails 3 and Devise.
I can have 2 types of Users I'm calling 'admin' and 'client', both stored in the Users table, differentiated by the value of the boolean 'admin' column.
An 'admin' can be created 2 ways:
1) Upon the creation of a new account - in this case we also create an associated 'business' record
2) From within an existing account (so one 'business' can have multiple 'admins') - in the case we want to associate it with an existing business record
A 'client' can be created 1 way:
1) From a form separate than those used to create an 'admin' above
In all 3 cases a different form will be used. And upon submission they all have different requirements:
- I want to send different emails to each.
- In the first 'admin' case above I need to check if the requested business url is unique, in the second case I don't. And in the client case I don't.
- In the first 'admin' case I want to create some default database entries upon completion of an account, but not in the second case.
- When a 'client' account is created I want to perform some other actions specific to that account type.
Should I use the same controller for the different User types, or separate them? If they should be the same, how does it know what type of User it should create?
What about the models?
Any thoughts on how to architect this?
开发者_运维百科Thanks so much.
My initial guess would be to create two subclasses from User
called Admin
and Client
. A bit like this:
class User < ActiveRecord::Base
end
class Admin < User
devise :database_authenticatable, :etc
after_create :create_business
validates_uniqueness_of :business_url
# etc
end
class Client < User
devise :database_authenticatable, :etc
# etc
end
Next, you can add different forms in config/routes.rb
:
YourApp::Application.routes.draw do
# for the cases that someone can create their own user
devise_for :clients
devise_for :admins
# for creating it when logged in as admin
namespace :backend do
resources :admins
end
end
Be sure to read up on STI in Rails and read the documentation of Devise.
You can use one controller for this with different actions and direct your forms to those actions:
form_tag(:controller => "people", :action => "search", :method => "get")
from rails form helpers.
精彩评论