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

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:

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.