Nil values being set in scaffold generated code for 1.2.3

Using rails 1.2.3 on mac os x with mongrel / safari and mysql 5 ( have
also tried against postgres 8.2 with the same results ). Encountering a
problem on the following scaffold generated code ( used in the
controller object which in this instance is managing the user model
object).
@user = User.new(params[:user])
The above line should populate a user object with values entered by the
client. Unfortunately the user object is populated with nil values, even
though the params user object has the client entered values. This seems
very unusual and more than likely an error on my part, but suggestions
about resolving the problem would be welcome.

Hi Andrew,

Andrew Don wrote:

@user = User.new(params[:user])
The above line should populate a user object with values entered by the
client. Unfortunately the user object is populated with nil values, even
though the params user object has the client entered values. This seems
very unusual and more than likely an error on my part, but suggestions
about resolving the problem would be welcome.

When you say the “user object is populated with nil values”, do you mean
the
Rails object, or the related record in the database? If the latter, my
first suggestion would be to check to see if there’s a validation that’s
keeping the User.save from succeeding. If there’s not an obvious (one
you
look) problem there, I’d suggest you post some additional code.

Best regards,
Bill

Hi Andrew,

Andrew Don wrote:

Thanks for responding.

You’re welcome.

of the user object were null.
Not sure what you’re using to look at the user object, but when you say
“all
the columns” it makes me wonder if you’re looking at the database rather
than the in-memory object.

There are model validations against the object in user.rb
and these are correctly validated and return a list of
failures , as the values in @user are all null.

To test whether the values in @user are, in fact, null, comment out the
save
and make a new view (just rename the old one) to be rendered by this
controller method. In it, just render the values of the @user
attributes
(e.g., <%= @user.name %>).

It would appear that there is a problem in the initialisation
of the @user object, but of course this assumption is surely
not correct.

Anything is possible, but I’d bet a nickle that the problem is in your
validations. They can get tricky. For starters, comment out all your
validations and see what gets saved. Then start adding the validations
back
to identify the one that’s giving you problems.

Best regards,
Bill

Bill W. wrote:

Hi Andrew,

Andrew Don wrote:

@user = User.new(params[:user])
The above line should populate a user object with values entered by the
client. Unfortunately the user object is populated with nil values, even
though the params user object has the client entered values. This seems
very unusual and more than likely an error on my part, but suggestions
about resolving the problem would be welcome.

When you say the “user object is populated with nil values”, do you mean
the
Rails object, or the related record in the database? If the latter, my
first suggestion would be to check to see if there’s a validation that’s
keeping the User.save from succeeding. If there’s not an obvious (one
you
look) problem there, I’d suggest you post some additional code.

Best regards,
Bill

Bill,
Thanks for responding. The code extract below is taken from the user
controller object and is scaffold generated code :
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = ‘User was successfully saved.’
etc etc.
I ran the code in debug mode setting a breakpoint just below the line
@user = User.new(params[:user])’ and found that values entered on the
browser were present in the params object but all the columns/attributes
of the user object were null. There are model validations against the
object in user.rb and these are correctly validated and return a list of
failures , as the values in @user are all null. It would appear that
there is a problem in the initialisation of the @user object, but of
course this assumption is surely not correct.

Bill W. wrote:

Hi Andrew,

Andrew Don wrote:

Thanks for responding.

You’re welcome.

of the user object were null.
Not sure what you’re using to look at the user object, but when you say
“all
the columns” it makes me wonder if you’re looking at the database rather
than the in-memory object.

There are model validations against the object in user.rb
and these are correctly validated and return a list of
failures , as the values in @user are all null.

To test whether the values in @user are, in fact, null, comment out the
save
and make a new view (just rename the old one) to be rendered by this
controller method. In it, just render the values of the @user
attributes
(e.g., <%= @user.name %>).

It would appear that there is a problem in the initialisation
of the @user object, but of course this assumption is surely
not correct.

Anything is possible, but I’d bet a nickle that the problem is in your
validations. They can get tricky. For starters, comment out all your
validations and see what gets saved. Then start adding the validations
back
to identify the one that’s giving you problems.

Best regards,
Bill

Bill,
Thanks again for your reply. I think you are absolutely right, the
problem lies somewhere in the object model validations . I’m in the
process of adding them back in one by one and I’ve already noticed that
the values are now being assigned as expected to the @user object.
… I’ve just finished adding all the object model validations back and
the @user object is behaving as expected i.e values assigned and
validations checked. Any failures are reported back to the client i.e
red outlined fields with previously entered values retained. A little
curious as to what exactly was the problem. I shall update post if I
find the exact reason behind the problem. But for now I’ll put it down
to an error I’ve made at some point in the process.
Thanks again for your help, much appreciated.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs