# Cannot register a new user

**URL:** <https://www.ruby-forum.com/t/cannot-register-a-new-user/144324>\
**Category:** Rails\
**Created:** [July 28, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/cannot-register-a-new-user/144324 "2008-07-28T17:51:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![invincibledevil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@invincibledevil](https://www.ruby-forum.com/u/invincibledevil)\
**Post date:** [July 28, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/cannot-register-a-new-user/144324/1 "2008-07-28T17:51:04Z")

</div>

Hello… I wanted to extend my webpage’s user interaction by allowing  
users to register themselves. The account controller method “register”  
takes care of the process. This is the register.rhtml code i wrote in  
apps/views/accounts/register.rhtml :

\<% form\_for :user do |f| %\>

login:  
 \<%= f.text\_field :login %\>

password:  
 \<%= f.password\_field :password %\>

name:  
 \<%= f.text\_field :name %\>

email:  
 \<%= f.text\_field :email %\>

\<%= submit\_tag %\>

\<% end %\>

The register method in the account controller looks like :

def register  
@u = User.new(params[:login])  
@u = @current\_user  
if request.post? and @u.save  
@current\_user = User.new(params[:login])  
flash[:notice] = ‘Registration succeeded’  
redirect\_to :controller =\> ‘story’, :action =\> ‘index’  
end  
end

The problem it gives a nil object error for the statement @u.save . I  
donot understand since when im breakpointing …$params shows that it  
holds the entire submitted form values …but $params[:login] =\> nil  
$params[:name] =\> nil

where is the all the data going and why is it not getting saved. please  
help

---

<div class="post-metadata">

**Author:** ![invincibledevil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@invincibledevil](https://www.ruby-forum.com/u/invincibledevil)\
**Post date:** [July 28, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/cannot-register-a-new-user/144324/2 "2008-07-28T18:05:13Z")

</div>

first you do:

> ```
> @u = User.new(params[:login])
> 
> ```

then you overwrite @u with:

> ```
> @u = @current_user
> 
> ```

but @current\_user is set later:

> ```
> @current_user = User.new(params[:login])
> 
> ```

so most likely @u = @current\_user sets @u to nil,  
since @current\_user isn’t set before that. In any case  
you would lose the newly created user.

And you should NEVER use variable names like @u  
that do not tell what they are good for…

---

<div class="post-metadata">

**Author:** ![invincibledevil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@invincibledevil](https://www.ruby-forum.com/u/invincibledevil)\
**Post date:** [July 28, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/cannot-register-a-new-user/144324/3 "2008-07-28T18:23:19Z")

</div>

Thorsten M. wrote:

> first you do:
> 
> > ï¿½ ï¿½ ï¿½@u = User.new(params[:login])
> 
> then you overwrite @u with:
> 
> > ï¿½ ï¿½ ï¿½@u = @current\_user
> 
> but @current\_user is set later:
> 
> > ï¿½ ï¿½ ï¿½ ï¿½@current\_user = User.new(params[:login])
> 
> so most likely @u = @current\_user sets @u to nil,  
> since @current\_user isn’t set before that. In any case  
> you would lose the newly created user.
> 
> And you should NEVER use variable names like @u  
> that do not tell what they are good for…

Thorsten…  
was getting bugged rewriting the name … so was using “u” … i know its  
a bad habit… so changed it…

anyways … i applied the changes and the story saves…thanks…  
but all the fields are being set to null. i mean the values the user  
enters for login password in registration page are all being set to null  
instead of what was enterd …  
weird … eh ?? is their a prob with the form\_for object ?

---

<div class="post-metadata">

**Author:** ![invincibledevil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@invincibledevil](https://www.ruby-forum.com/u/invincibledevil)\
**Post date:** [July 28, 2008, 7:08pm UTC](https://www.ruby-forum.com/t/cannot-register-a-new-user/144324/4 "2008-07-28T19:08:49Z")

</div>

Thanks… its working 🙂 yes i guess i was hung up on login than the  
actual object name “user”

User.find\_by\_login(params[:login]) should also work fine then !

---

<div class="post-metadata">

**Author:** ![invincibledevil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@invincibledevil](https://www.ruby-forum.com/u/invincibledevil)\
**Post date:** [July 28, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/cannot-register-a-new-user/144324/5 "2008-07-28T18:50:22Z")

</div>

no, there is no problem with form\_for  
but you’re using params[:login] instead of params[:user]  
a form like this has a params hash named as the model the form is made  
for  
the single fields are used then like:  
params[:user][:login]  
in your case  
User.new(params[:user])  
should work

---

<div class="post-metadata">

**Author:** ![invincibledevil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@invincibledevil](https://www.ruby-forum.com/u/invincibledevil)\
**Post date:** [July 28, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/cannot-register-a-new-user/144324/6 "2008-07-28T19:24:55Z")

</div>

> User.find\_by\_login(params[:login]) should also work fine then !

If it has it’s own form then maybe.  
But more likely you have a form with some name and some fields.

so if the forms name is login and the fieldname is username then  
User.find\_by\_login(params[:login][:username])

or if the form is named user again and the textfield is login, then:  
User.find\_by\_login(params[:user][:login])

because find\_by\_login expects the single string, while  
User.new expects a hash with all the users attributes.
