# \*Please Help\* Cannot save a new user to the database

**URL:** <https://www.ruby-forum.com/t/please-help-cannot-save-a-new-user-to-the-database/174549>\
**Category:** Rails\
**Created:** [August 28, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/please-help-cannot-save-a-new-user-to-the-database/174549 "2009-08-28T15:16:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [August 28, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/please-help-cannot-save-a-new-user-to-the-database/174549/1 "2009-08-28T15:16:35Z")

</div>

Hello,

I am following the RailsSpace tutorial and am at the point where I am  
creating a new user through (user/register)

I have added the details into the form: screen\_name, email and  
password.

When I click register, I would expect this to be saved (commited) to  
the database.

However – when I look in the terminal, it says ‘Rollback’ instead of  
‘COMMIT’

The code in the user controller looks like this:

class UserController \< ApplicationController

def register  
@title = “Register”  
if request.post? and params[:user]  
@user = User.new(params[:user])  
if @user.save  
flash[:notice] = “User #{@user.screen\_name} created!”  
redirect\_to :action =\> “index”  
end  
end  
end

Does anyone know why in my terminal it is not saving this as I’d  
expect?

Many Thanks

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [August 28, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/please-help-cannot-save-a-new-user-to-the-database/174549/2 "2009-08-28T15:54:39Z")

</div>

2009/8/28 RubyonRails\_newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> the database.  
> Â Â if request.post? and params[:user]  
> Â Â Â @user = User.new(params[:user])  
> Â Â Â if @user.save  
> Â Â Â Â flash[:notice] = “User #{@user.screen\_name} created!”  
> Â Â Â Â redirect\_to :action =\> “index”

I would expect see something like this here, for the case when the save  
fails  
else  
render :action =\> “new or whatever is is the action that  
posted to register”

> Â Â Â end  
> Â Â end  
> Â end
> 
> Does anyone know why in my terminal it is not saving this as Iâ€™d  
> expect?

Maybe your validations are failing, if you have the else condition  
above and then in the new action (or whatever it is) inside the  
form\_for you should have \<%= f.error\_messages %\> and the errors will  
appear.

Colin

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [August 28, 2009, 4:02pm UTC](https://www.ruby-forum.com/t/please-help-cannot-save-a-new-user-to-the-database/174549/3 "2009-08-28T16:02:51Z")

</div>

cheers Colin - i’ll try this out.

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [August 28, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/please-help-cannot-save-a-new-user-to-the-database/174549/4 "2009-08-28T17:25:25Z")

</div>

… the strange thing is, the code I have written is axactly the same  
as that in the railsspace website…  
([Railsspace.com is for sale | HugeDomains](http://railsspace.com/4_register/listing-4-20.txt))

On 28 Aug, 14:33, RubyonRails\_newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [August 28, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/please-help-cannot-save-a-new-user-to-the-database/174549/5 "2009-08-28T18:17:31Z")

</div>

On Fri, Aug 28, 2009 at 5:20 AM,  
RubyonRails\_newbie[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have added the details into the form: screen\_name, email and  
> password.
> 
> When I click register, I would expect this to be saved (commited) to  
> the database.

And there’s no specific error in the log? That’s surprising.

I would try this in the Rails console, using @user.save! to show any  
errors being generated.

You could also look at your DB query and/or error logs for more info.

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [August 28, 2009, 10:24pm UTC](https://www.ruby-forum.com/t/please-help-cannot-save-a-new-user-to-the-database/174549/6 "2009-08-28T22:24:33Z")

</div>

2009/8/28 RubyonRails\_newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> … the strange thing is, the code I have written is axactly the same  
> as that in the railsspace website…  
> ([Railsspace.com is for sale | HugeDomains](http://railsspace.com/4_register/listing-4-20.txt))

Could you bottom post rather than top? That is the convention on this  
list, it makes it easier to follow threads.

Do they go on to extend this code to cope with save failures later on  
in the tutorial? If you just want to get past this for the moment  
have a close look at any validations in the User model, possibly  
comment them out for the moment. Also have a look at the log  
(log/development.log) to see what values were passed as params in the  
POST. This may be a good time to have a look at ruby-debug (see  
[#54 Debugging with ruby-debug - RailsCasts](http://railscasts.com/episodes/54-debugging-with-ruby-debug)). You  
could put a break point after @user = User.new(params[:user]) and  
check that it looks ok.

Colin

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [August 29, 2009, 6:37pm UTC](https://www.ruby-forum.com/t/please-help-cannot-save-a-new-user-to-the-database/174549/7 "2009-08-29T18:37:19Z")

</div>

As others have pointed out, there’s a validation failing on your User  
model.

I’d also point out that RailsSpace may not be a good book to start  
with, as it is positively ancient as Rails books code. The printed  
code is targeted at version 1.2.3, which is over 2 years old now.  
You’ll be better served, I’d assume, with a newer tutorial.

–Matt J.

On Aug 28, 8:20 am, RubyonRails\_newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
