ruby-on-railsclearance

Rails 5: Clearance - User CRUD?


I am currently using https://github.com/thoughtbot/clearance for authentication.

It allows me to sign-up & sign-in using password and email.

But I was wondering how I can configure it to have a CRUD pages for the generated users model, because I actually want to see a list of registered users.


Solution

  • You can use a regular Users controller, subclassed from clearance.

    class UsersController < Clearance::UsersController
      def index
        @logged_in_users = User.where(blah) #whatever logic you need to retrieve the list of users 
      end
    end
    

    I created my Users controller first, then ran the clearance generator, and then the routes generator. After generating the default routes, you can modify to point to your own controller.

    rails g clearance:install
    rails g clearance:routes
    
    
      resources :users, controller: "users" do
        resource :password,
        controller: "clearance/passwords",
        only: [:create, :edit, :update]
      end
    
    get "/sign_in" => "clearance/sessions#new", as: "sign_in"
    delete "/sign_out" => "clearance/sessions#destroy", as: "sign_out"
    get "/sign_up" => "clearance/users#new", as: "sign_up"