# Crypt\_unless\_empty (login generator)

**URL:** <https://www.ruby-forum.com/t/crypt-unless-empty-login-generator/48276>\
**Category:** Rails\
**Created:** [November 18, 2005, 2:05pm UTC](https://www.ruby-forum.com/t/crypt-unless-empty-login-generator/48276 "2005-11-18T14:05:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam.groves](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/cab0a1/32.png) [@adam.groves](https://www.ruby-forum.com/u/adam.groves)\
**Post date:** [November 18, 2005, 2:05pm UTC](https://www.ruby-forum.com/t/crypt-unless-empty-login-generator/48276/1 "2005-11-18T14:05:47Z")

</div>

I have a form for users to change their account details, including their  
password.  
Here’s my form:

First name(s)  
  
\<%= text\_field ‘user’, ‘first’ %\>  
  
Surname  
  
\<%= text\_field ‘user’, ‘name’ %\>  
  
Telephone  
  
\<%= text\_field ‘user’, ‘tel’ %\>  
  
email  
  
\<%= text\_field ‘user’, ‘email’ %\>

Login  
\<%= text\_field 'user', 'login' %\>  
Choose password:  
  
Confirm password:  
  
 and here's my controller method: def update\_user @user = User.find(params[:id]) @project = Project.find(params[:project]) @company = Company.find(params[:company]) if @request.post? and @user.update\_attributes(params[:user]) User.authenticate(@user.login, @params[:user][:password]) flash[:notice] = 'User was successfully updated' redirect\_back\_or\_default :action =\> 'index', :project =\> @project, :company =\> @company end end It's all working well and the errors are listed if fields don't validate. However, I want the update to be successful if the two password fields are left empty (user doesn't want to change their password). according to the comments in the user model, the method crypt\_unless\_empty should catch this before update and I presume allow the record through. This isn't working for some reason and I get 3 errors: - Password confirmation can't be blank - Password can't be blank - Password is too short (min is 5 characters)

Does anyone have any suggestions as to why this doesn’t work (and how I  
can  
get it working)?  
Regards  
Adam  
PS: Has anyone used the login generator in a commercial rails  
application?

---

<div class="post-metadata">

**Author:** ![adam.groves](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/cab0a1/32.png) [@adam.groves](https://www.ruby-forum.com/u/adam.groves)\
**Post date:** [January 4, 2006, 12:15am UTC](https://www.ruby-forum.com/t/crypt-unless-empty-login-generator/48276/2 "2006-01-04T00:15:04Z")

</div>

adam.groves wrote:

> I have a form for users to change their account details, including their  
> password.  
> Here’s my form:
> 
> First name(s)  
>   
> \<%= text\_field ‘user’, ‘first’ %\>  
>   
> Surname  
>   
> \<%= text\_field ‘user’, ‘name’ %\>  
>   
> Telephone  
>   
> \<%= text\_field ‘user’, ‘tel’ %\>  
>   
> email  
>   
> \<%= text\_field ‘user’, ‘email’ %\>
> 
> Login  
> \<%= text\_field 'user', 'login' %\>  
> Choose password:  
>   
> Confirm password:  
>   
> and here's my controller method: def update\_user @user = User.find(params[:id]) @project = Project.find(params[:project]) @company = Company.find(params[:company]) if @request.post? and @user.update\_attributes(params[:user]) User.authenticate(@user.login, @params[:user][:password]) flash[:notice] = 'User was successfully updated' redirect\_back\_or\_default :action =\> 'index', :project =\> @project, :company =\> @company end end It's all working well and the errors are listed if fields don't validate. However, I want the update to be successful if the two password fields are left empty (user doesn't want to change their password). according to the comments in the user model, the method crypt\_unless\_empty should catch this before update and I presume allow the record through. This isn't working for some reason and I get 3 errors: - Password confirmation can't be blank - Password can't be blank - Password is too short (min is 5 characters)
> 
> Does anyone have any suggestions as to why this doesn’t work (and how I  
> can  
> get it working)?  
> Regards  
> Adam  
> PS: Has anyone used the login generator in a commercial rails  
> application?
> 
> I’m having a very similar problem and haven’t been able to find a  
> solution.
> 
> We want users to be able to edit their settings WITHOUT having to  
> re-enter their passwords, so we’re using before\_update  
> :crypt\_unless\_empty to properly populate the password field of the  
> object to be updated.
> 
> Unfortunately, it would seem that the object to be updated attempts to  
> validate its fields BEFORE applying the before\_update directives, so  
> blank passwords are invalidated before they can be corrected by the  
> crypt\_unless\_empty function. This seems like really poor behavior but  
> it’s all I can come up with to explain what we’re seeing. Any advice is  
> greatly appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![adam.groves](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/cab0a1/32.png) [@adam.groves](https://www.ruby-forum.com/u/adam.groves)\
**Post date:** [January 21, 2006, 8:36am UTC](https://www.ruby-forum.com/t/crypt-unless-empty-login-generator/48276/3 "2006-01-21T08:36:13Z")

</div>

hai

---

<div class="post-metadata">

**Author:** ![adam.groves](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/cab0a1/32.png) [@adam.groves](https://www.ruby-forum.com/u/adam.groves)\
**Post date:** [January 21, 2006, 8:38am UTC](https://www.ruby-forum.com/t/crypt-unless-empty-login-generator/48276/4 "2006-01-21T08:38:41Z")

</div>

hai

Modify ur model class by including the lines below

validates\_uniqueness\_of :login, :on =\> :update  
validates\_confirmation\_of :password, :on =\> :update  
validates\_length\_of :login, :within =\> 3…40, :on =\> :update  
validates\_length\_of :password, :within =\> 5…10, :on =\> :create  
validates\_presence\_of :login, :password, :password\_confirmation ,:on  
=\> :create  
validates\_confirmation\_of :password  
if !([:password].empty?)  
User.validates\_length\_of :password, :within =\> 5…10  
end

```
		attr_accessor :password
 		attr_accessible :name, 

```

:password,:first\_name,:last\_name,:office\_phone,:mobile\_phone,:email,:login,:password\_confirmation  
before\_create :crypt\_password

```
		before_update :crypt_unless_empty

```

sure!!! It will work

---

<div class="post-metadata">

**Author:** ![adam.groves](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/cab0a1/32.png) [@adam.groves](https://www.ruby-forum.com/u/adam.groves)\
**Post date:** [February 1, 2006, 1:34am UTC](https://www.ruby-forum.com/t/crypt-unless-empty-login-generator/48276/5 "2006-02-01T01:34:10Z")

</div>

On Saturday 21 Jan 2006 09:32, Alex Rudnitski wrote:

> But I have the other problem: how to make password field to be blank by  
> default, on update user detalis page? Not 40 digits hash pussword.

I initially got this working by using password\_field\_tag rather than  
password\_field on my form, so it just creates the blank password fields  
and  
never puts the values from the model in them. Combine that with the  
crypt\_unless\_empty stuff, and you have quite a neat solution, so for  
example,  
in my form where the user can update their own password I had (THIS IS  
NOT MY  
FINAL SOLUTION):

Password  
\<%= password\_field\_tag 'user[password]' %\>

Confirm Password  
\<%= password\_field\_tag 'user[password\_confirmation]' %\>

Whenever there is a model error and you have to re-enter something, the  
password fields do not retain what you just typed in them, meaning you  
have  
to type it again. I think this is a fairly standard thing to do - or at  
least, hardly unexpected behaviour? (Net result if you then didn’t type  
anything in before updating would be that the password would not change,  
so  
no major disaster there.)

HOWEVER, in order to trick the model into accepting these parameters,  
you have  
to include the square brackets in the fieldname, which then gives you  
invalid  
IDs in your HTML. I couldn’t spot a way to manually set the ID on  
password\_field\_tag, so instead I just went for the simplest option,  
which was  
to specify the HTML directly instead of using helpers go generate it:

Password

Confirm Password

I’m not sure if that would be considered an un-Rails-like way of doing  
it, but  
it’s extremely simple and it works, so I don’t think it’s too much of a  
sin  
overall, unless anyone thinks otherwise? It follows the KISS principle  
at  
least. 😉

Cheers,

~Dave

–

Dave S.  
Rent-A-Monkey Website Development

> **[Tree Removal Service For Home & Businesses in Salt Lake City, UT](https://www.rentamonkey.com/)**
>
> Rent A Monkey Tree Service provides the safest, highest-quality tree removal for Salt Lake City, UT homes and businesses. Request a service today!

PGP Key: [http://www.rentamonkey.com/pgpkey.asc](http://www.rentamonkey.com/pgpkey.asc)

---

<div class="post-metadata">

**Author:** ![adam.groves](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/cab0a1/32.png) [@adam.groves](https://www.ruby-forum.com/u/adam.groves)\
**Post date:** [February 1, 2006, 1:40am UTC](https://www.ruby-forum.com/t/crypt-unless-empty-login-generator/48276/6 "2006-02-01T01:40:12Z")

</div>

On Wednesday 01 Feb 2006 00:32, Dave S. wrote:

> Whenever there is a model error

D’uh, I meant a “validation” error, not a “model” error… excuse the  
brainfart!

~Dave

–

Dave S.  
Rent-A-Monkey Website Development

> **[Tree Removal Service For Home & Businesses in Salt Lake City, UT](https://www.rentamonkey.com/)**
>
> Rent A Monkey Tree Service provides the safest, highest-quality tree removal for Salt Lake City, UT homes and businesses. Request a service today!

PGP Key: [http://www.rentamonkey.com/pgpkey.asc](http://www.rentamonkey.com/pgpkey.asc)

---

<div class="post-metadata">

**Author:** ![adam.groves](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/cab0a1/32.png) [@adam.groves](https://www.ruby-forum.com/u/adam.groves)\
**Post date:** [January 21, 2006, 10:32am UTC](https://www.ruby-forum.com/t/crypt-unless-empty-login-generator/48276/7 "2006-01-21T10:32:36Z")

</div>

Adam G., I’m also working on login system, the solution you  
searching could be found here [Problem with updating model - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/52250) I  
make checking if the password field is blank in controller

def edit  
@edituser = User.find\_first([“id = ?”,params[:id]])  
if @request.post?  
if not @edituser.nil?  
@edituser.login = @params[:edituser][:login]  
@edituser.birthday = @params[:edituser][:birthday]  
@edituser.login = @params[:edituser][:login]  
if not @params[:edituser][:password].nil? and  
@params[:edituser][:password] ==  
@params[:edituser][:password\_confirmation] and  
@params[:edituser][:password] != “”  
@edituser.password = @params[:edituser][:password]  
end  
end  
end  
end

But I have the other problem: how to make password field to be blank by  
default, on update user detalis page? Not 40 digits hash pussword. Also  
I have problem with updating user birthday date, can anybody tell the  
raason? Full dascription here [Problem with updating model - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/52250)
