# Rails Recipes #31 Authenticating Users

**URL:** <https://www.ruby-forum.com/t/rails-recipes-31-authenticating-users/87771>\
**Category:** Rails\
**Created:** [March 5, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/rails-recipes-31-authenticating-users/87771 "2007-03-05T21:50:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [March 5, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/rails-recipes-31-authenticating-users/87771/1 "2007-03-05T21:50:06Z")

</div>

I have figured out everything except for how to add a new user to the  
database. The book gives an example of adding a user through the  
command line, and that works fine. I can login using a user created  
that way. I cannot, however, figure out how to add a new user from a  
form. The problem seems to occur when trying to except a user submitted  
password (of which there is no corresponding DB listing) and generating  
a salt and hash to be stored in the DB. The method in the user model:

def password=(pass)  
salt = [Array.new(6){rand(256).chr}.join].pack(“m”).chomp  
self.password\_salt, self.password\_hash = salt,  
Digest::SHA256.hexdigest(pass + salt)  
end

how do I accept a password from a view from a form, generate the  
password and salt and save it to the DB. Here is what I have for the  
view form

View:

\<%= form\_tag({:action =\> ‘new\_user’}, :multipart =\> true) %\>

**Username:**  
 \<%= text\_field("user", "username", "size" =\> 20 ) %\>

**Password:**  
 \<%= text\_field\_tag("password",nil, "size" =\> 20 ) %\>

\<%= submit\_tag(" Register ")%\>  
\<%= end\_form\_tag %\>

I changed the text\_field to text\_field\_tag because the fact that  
password is not a line in the DB was raising an error. I can’t figure  
out how to handle this in the controller in :action =\> “new\_user”

Thanks

chris

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [March 5, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/rails-recipes-31-authenticating-users/87771/2 "2007-03-05T23:09:06Z")

</div>

On Mon, 2007-03-05 at 21:50 +0100, chris wrote:

> ```
> self.password_salt, self.password_hash = salt,
> 
> ```
> 
> \<%= submit\_tag(" Register ")%\>  
> \<%= end\_form\_tag %\>
> 
> I changed the text\_field to text\_field\_tag because the fact that  
> password is not a line in the DB was raising an error. I can’t figure  
> out how to handle this in the controller in :action =\> “new\_user”

* * *

\<%= form\_tag({:action =\> ‘new\_user’}, :multipart =\> true) %\>

**Username:**  
 \<%= text\_field("user", "username", "size" =\> 20 ) %\>

**Password:**  
 \<%= text\_field("user", "password", "size" =\> 20 ) %\>

\<%= submit\_tag(" Register ")%\> \<%= end\_form\_tag %\>

I don’t know what ‘:multipart =\> true’ does for you

–  
Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
