How to properly debug?


#1

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.


#2

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


#3

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.


#4

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