Admin user administration with Devise

前端 未结 2 951
终归单人心
终归单人心 2021-02-04 13:50

I am trying out Devise for the first time. One of the things that I wanted to do is provide an interface for Admin users to create, find and edit users. Here\'s where I may have

2条回答
  •  鱼传尺愫
    2021-02-04 14:08

    Thank you very much for the help. This is essentially exactly what I am doing. I discovered a clue that helped me solve the problem of the user's session being cleared when they edit their own record in this wiki:

    https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

    This is the line I needed:

    sign_in resource_name, resource, :bypass => true
    

    This method is located in Devise::Controllers::Helpers so I did this in my controller.

    class PeopleController < ApplicationController
       include Devise::Controllers::Helpers
    

    Then in my update method I call it only if the current_user.id equals the id that is being edited:

    def update
      @person = User.find(params[:id])
      if @person.update_attributes(params[:user])
        sign_in @person, :bypass => true if current_user.id == @person.id
        redirect_to  person_path(@person), :notice  => "Successfully updated user."
      else
        render :action => 'edit'
      end
    end
    

    Now if the current user edits their own record, the session is restored after it is saved.

    Thanks again for your responses.

提交回复
热议问题