DivineRails

Ruby on Rails

Blog

Devise Authentication (Update record without providing current password)

Posted by Rq Bukhari on March 7, 2012 at 1:30 AM




In this post I'm going to introduce devise authentication in ruby on rails. Devise is a full-featured authentication solution for your application. It handles all the controller logic and views. To learn how to use devise there are many tutorials, screen casts available. I'm not going to share anything about those tutorials. Those who want to know the use of devise they can learn it from the links below:


Devise provides the facility to override controllers, to generate views and modify them. All these are explained in the above links. Here I'm going to share a different thing. Here I'm going to show you how to update user details without providing current password. When I was performing the functionality I searched everything but found no solution for the problem. At last I tried a solution and it worked.


My client's requirement was to update user details from admin panel. Where the admin will update user details without providing user password. Even admin can change the user password. To do so I updated the user attributes one by one because using the update_attributes will not work for you.


def update

  respond_to do |format|

    if @user.update_attribute('first_name', params[:user][:first_name]) &&

       @user.update_attribute('last_name', params[:user][:last_name]) &&

       @user.update_attribute('email', params[:user][:email])

      format.html {

        redirect_to(admin_user_details_path, :notice => 'Record updated Successfully.')

      }

    else

      format.html { render :action => "edit" }

    end

  end

end


This way you can update the firstname, lastname and email of a user without providing current password in devise authentication system.

I hope you like the post and you find it helpful.

Categories: None

Post a Comment

Oops!

Oops, you forgot something.

Oops!

The words you entered did not match the given text. Please try again.

Already a member? Sign In

2 Comments

Reply Jamal Nasir
7:43 AM on November 26, 2012 
I think you can also do it like this:

if @user.update_attributes(:first_name = params[:user][:first_name],
:last_name = params[:user][:last_name],
:email = params[:user][:email])

.
.
.

end

In this case the update process is called just once.

I have just replaced the condition.
Reply Rq Bukhari
8:00 AM on November 26, 2012 
Jamal Nasir says...
I think you can also do it like this:

if @user.update_attributes(:first_name = params[:user][:first_name],
:last_name = params[:user][:last_name],
:email = params[:user][:email])

.
.
.

end

In this case the update process is called just once.

I have just replaced the condition.

Yes we can do it that way too..
thanks for the suggestion.. :)