How To: Manage users through a CRUD interface

William Mathewson edited this page Aug 15, 2016 · 3 revisions
Clone this wiki locally

Devise is great because of its flexibility. Performing basic CRUD actions on the User model is actually no different than creating CRUD actions for anything else in Rails.

There are a couple things to remember though:

Make sure to put your resources :users below the devise_for :users route.

Since the registration routes and user managing routes can conflict, you need to change one of them. You can either put devise under a specific prefix:

devise_for :users, :path_prefix => 'my'
resources :users

Or your users

devise_for :users
scope "/admin" do
  resources :users

In your UsersController, you will also need to remove the password key of the params hash if it's blank. If not, Devise will fail to validate. Add something like this to your update method:

if params[:user][:password].blank?

Examples of CRUD Actions in UsersController

def destroy

  respond_to do |format|
    format.json { respond_to_destroy(:ajax) }
    format.xml  { head :ok }
    format.html { respond_to_destroy(:html) }      

# enter other examples of other CRUD actions below: