Skip to content

Customizing Routes and User classes for Roles

zaiste edited this page Feb 18, 2011 · 4 revisions

Cream since version 0.8.6 also assists you in setting up customized routing for registrations if you need it.
For this to work, there must be a model file or each role using this approach.

Example role model file for editor role:

class Editor < User
  ...
end

Cream creates such model files by default. You can then easily customize each type of user, adding relevant class and instance methods for great flexibility. This also lets you easily customize devise strategies for each role (see devise).

Cream’s default routes configuration

For each role (except guest), Cream inserts the following code in the routes file:

Example for editor role:

devise_for :editors, :class_name => 'Editor'
as :editor do
  match "/editors/sign_up" => "devise/registrations#new", :as => :#editor_signup
end

The example code uses the default devise configuration, where all users are sent to the same built-in registrations controller of devise, using the same #new REST action.

Simple customization

If you need an individual registration page for one or more roles, you can customize which action the matching route points to.

  match "/editors/sign_up" => "user_registrations#new_editor", :as => :#editor_signup

This would require a controller file user_registrations_controller.rb with an action method #new_editor.

Example action

def new_editor
  build_resource({})
end

The resource would be built and ready for a form and the default rendering is used, expecting a view file new_editor in views/user_registrations/

Advanced customization using role specific namespaces

For more advanced scenarios you could choose to create a separate controller namespace for one or more roles:

  match "/editors/sign_up" => "editor/registrations#new", :as => :#editor_signup

This would require a controller file editor/registrations_controller.rb with an action method #new. The namespace option often makes sense at least for the admin role. Here the view file would be new in views/editors/user_registrations/.