Skip to content

How To: Manage users through a CRUD interface

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

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
end

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?
  params[:user].delete(:password)
  params[:user].delete(:password_confirmation)
end

Examples of CRUD Actions in UsersController

def destroy
  @user.destroy!

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

# enter other examples of other CRUD actions below:

Clone this wiki locally