[Rails] Re: crypt_unless_empty (login generator)

Dave Silvester dave at rentamonkey.com
Wed Feb 1 00:32:26 GMT 2006


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):

<p><label for="user_password">Password</label><br/>
<%= password_field_tag 'user[password]' %></p>

<p><label for="user_password_confirmation">Confirm Password</label><br/>
<%= password_field_tag 'user[password_confirmation]' %></p>


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:


<p><label for="user_password">Password</label><br/>
<input id="user_password" name="user[password]" type="password" /></p>

<p><label for="user_password_confirmation">Confirm Password</label><br/>
<input id="user_password_confirmation" name="user[password_confirmation]" 
type="password" /></p>


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 Silvester
Rent-A-Monkey Website Development
http://www.rentamonkey.com/

PGP Key: http://www.rentamonkey.com/pgpkey.asc


More information about the Rails mailing list