# How to properly debug?

**URL:** <https://www.ruby-forum.com/t/how-to-properly-debug/49127>\
**Category:** Ruby\
**Created:** [November 30, 2005, 10:08am UTC](https://www.ruby-forum.com/t/how-to-properly-debug/49127 "2005-11-30T10:08:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![miriamraphael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@miriamraphael](https://www.ruby-forum.com/u/miriamraphael)\
**Post date:** [November 30, 2005, 10:08am UTC](https://www.ruby-forum.com/t/how-to-properly-debug/49127/1 "2005-11-30T10:08:43Z")

</div>

I am writing my first rails script and am having lots of trouble doing  
the simplest tasks.  
Right now, I have a form (new.rhtml) and it allows someone to become a  
new user by entering their info (username, password, name, etc).

the form calls the ‘create’ routine which is supposed to save it to  
the DB. However, the data is not being saved. How can I debug this? I  
am a perl scripter and with Perl I know how to find out where the bugs  
are, but because I’m new to ruby/rails, I can’t figure this out.

Here is the form:

| Please choose a username and password to create your account |
| User Name\*: | |
| Password\*: | |
| Account Information |
| Company Name\*: | |
| First Name\*: | |
| Last Name\*: | |
| Email Address\*: | |
| Address 1\*: | |
| Address 2: | |
| City\*: | |
| Zip Code\*: | |
| Phone\*: | |
| Mobile\*: | |
| |

Here is my user controller with the create routine:

class UserController \< ApplicationController  
ActiveRecord::Validations::ClassMethods  
scaffold :user

```
def new
@user = User.new
@mystates = State.find_all
end

```

def create  
@newuser = User.new(@params[‘user’])  
@newuser.creation\_date = Date.today  
@newuser.last\_login = Date.today  
@newuser.save  
end

end

Here is my user table definition:

create table users  
(  
id int auto\_increment not null unique,  
username varchar(15) not null,  
password varchar (16) not null,  
company\_name varchar(25) not null,  
first\_name varchar(25) not null,  
last\_name varchar(25) not null,  
email varchar(35) not null,  
address1 varchar(40) not null,  
address2 varchar(40) ,  
city varchar(35) not null,  
state tinyint ,  
zip varchar(12) not null,  
phone varchar(15) not null,  
mobile varchar(15) ,  
creation\_date date,  
last\_login datetime,  
primary key (id), index(id, state)  
);

Just a note that the state was giving me problems (error said that I  
was trying to put in a string when it needed an int) so I removed it  
from the form.

Any idea why this isn’t working? What can I insert in my ‘create’  
method so that I can see what the problem is? I need to learn how to  
debug these things myself so that I don’t have to bother user groups  
like this one. Just a note that I can see the ‘contents’ of the @params  
array. It’s not helping my debugging though.  
Thank you in advance.

---

<div class="post-metadata">

**Author:** ![miriamraphael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@miriamraphael](https://www.ruby-forum.com/u/miriamraphael)\
**Post date:** [November 30, 2005, 3:33pm UTC](https://www.ruby-forum.com/t/how-to-properly-debug/49127/2 "2005-11-30T15:33:14Z")

</div>

On Wed, Nov 30, 2005 at 06:07:31PM +0900, removed\_email\_address@domain.invalid wrote:

> I am writing my first rails script and am having lots of trouble doing  
> the simplest tasks.

I don’t see anything obviously wrong. But here are two debugging  
suggestions:

1. Look in log/development.log

2. Try running the environment in a console and creating a new record  
“by hand”:

$ ruby script/console development  
Loading development environment.

> > user = User.new({‘name’ =\> ‘whatever’, ‘pass’ =\> ‘whatever’})  
> > user.save

And finally, try asking your question on the Rails mailing list.  
You’ll get better support there.

regards,  
Ed

---

<div class="post-metadata">

**Author:** ![miriamraphael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@miriamraphael](https://www.ruby-forum.com/u/miriamraphael)\
**Post date:** [November 30, 2005, 5:43pm UTC](https://www.ruby-forum.com/t/how-to-properly-debug/49127/3 "2005-11-30T17:43:17Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I am writing my first rails script and am having lots of trouble doing  
> the simplest tasks.  
> Right now, I have a form (new.rhtml) and it allows someone to become a  
> new user by entering their info (username, password, name, etc).

Use .save! instead of .save.

.save returns false on failure, .save! raises an Exception.

---

<div class="post-metadata">

**Author:** ![miriamraphael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@miriamraphael](https://www.ruby-forum.com/u/miriamraphael)\
**Post date:** [November 30, 2005, 7:16pm UTC](https://www.ruby-forum.com/t/how-to-properly-debug/49127/4 "2005-11-30T19:16:31Z")

</div>

Yeah, or my preferred pattern:

unless user.valid?  
flash[:error] = “Possibly something custom”  
redirect or render something  
return  
end  
user.save

Actually, now that I’ve written that out, maybe save! and rescue is more  
compact

user.save!  
rescue WhateverThatExceptionIsCalled =\> e  
redirect\_somewhere  
end
