Forum: Ruby How to properly debug?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
miriamraphael (Guest)
on 2005-11-30 11:08
(Received via mailing list)
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:

<form action="/user/create" method="post">
<table width=400>
<tr bgcolor="#999999"><td colspan=2>Please choose a username and
password to create your account</tr>

<tr><td>User Name*:</td><td><input id="username" name="user[username]"
maxlength=12 type=text></tr>
<tr><td>Password*:</td><td><input id="password" name="user[password]"
maxlength=12 type=password></tr>

<tr bgcolor="#999999"><td colspan=2>Account Information</td></tr>
<tr><td>Company Name*:</td><td><input id="company_name"
name="user[company_name]" maxlength=25 type=text></td></tr>
<tr><td>First Name*:</td><td><input id="first_name"
name="user[first_name]" maxlength=25 type=text></td></tr>
<tr><td>Last Name*:</td><td><input id="last_name"
name="user[last_name]" maxlength=25 type=text></td></tr>
<tr><td>Email Address*:</td><td><input id="email" name="user[email]"
maxlength=35 type=text></td></tr>
<tr><td>Address 1*:</td><td><input id="address1" name="user[address1]"
maxlength=40 type=text></td></tr>
<tr><td>Address 2:</td><td><input id="address2" name="user[address2]"
maxlength=40 type=text></td></tr>
<tr><td>City*:</td><td><input id="city" name="user[city]" maxlength=35
type=text></td></tr>

<tr><td>Zip Code*:</td><td><input id="zip" name="user[zip]"
maxlength=12 type=text></td></tr>
<tr><td>Phone*:</td><td><input id="phone" name="user[phone]"
maxlength=15 type=text></td></tr>
<tr><td>Mobile*:</td><td><input id="mobile" name="user[mobile]"
maxlength=15 type=text></td></tr>
<tr><td colspan=2><input type="submit" value= "Save and
Continue"></td></tr>
</form>
</table>

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.
ef (Guest)
on 2005-11-30 16:33
(Received via mailing list)
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
florgro (Guest)
on 2005-11-30 18:43
(Received via mailing list)
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.
wilsonb (Guest)
on 2005-11-30 20:16
(Received via mailing list)
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
This topic is locked and can not be replied to.