Showstopper - can't save a record

Shauna wrote:

Page Source):
<li><label for="payment_trans_date">Date: </label>
<select name="payment[trans_date(1i)]">

Curious. What is 'trans_date(1i)' ? I have not seen this in so far. Usually it would
have been just 'payment[trans_date]'.

payments_controller.rb:
  def new
...
  end

  def create
    @borrower = Borrower.find(:first, :conditions => "id = " +
session[:borrowerid].to_s )
    @loan = Loan.find(:first, :conditions => "id = " +
session[:loanid].to_s )
    begin
      @payment = Payment.new(params[:id])
      @payment.loan_id = session[:loanid]
      @payment.PmtPeriodNum = session[:pmtpernum]
# @payment.trans_date = Date.today # this test works (record
posts with today's date)

I don't see a @payment.attributes = params[:payment] here so that the object is
updated with the form attributes.

      if @payment.save
....

HTH,

Long
www.edgesoft.ca

I'm not quite clear on the differences between
  @payment = Payment.new

This creates an empty Payment object

  @payment = Payment.new(params[:id])

This should give you an error..

  @payment.attributes = params[:payment]

This takes an existing payment object (maybe initialized as your first
line) and sets all its attributes to the corresponding values in the
hash

  @payment = Payment.new(params[:payment])

This creates a new payment object with its attributes initialized to
the corresponding values in the hash

The easiest is just to do
@payment = Payment.new params[:payment]

The problem with your code was that you were doing
Payment.new(params[:id]). That's similar to calling something like
Payment.new(5) which doesn't really make sense.

Pat

Also, it's a good idea to include whatever code you think could be
related the problem. We'll be able to help you more quickly, as it's
far more likely to be an error in your code than a bug in Rails itself
:slight_smile:

Pat

Shauna wrote:

Long wrote:
> I don't see a @payment.attributes = params[:payment] here so that the
> object is
> updated with the form attributes.
>
> HTH,
>
> Long
> www.edgesoft.ca

Long, that fixed the logjam! Now when I get back from my trip I'll back
out of my changes one by one till I'm back at TransDate with the proper
formatting, and see if anything breaks along the way.

I'm not quite clear on the differences between
  @payment = Payment.new

Gives you an uninitialized instance

  @payment = Payment.new(params[:id])

Gives you another uninitialized instance...I think.

  @payment.attributes = params[:payment]

Updates @payment 'fields' with corresponding 'values' in the params hash

  @payment = Payment.new(params[:payment])

Gives you a new and initialized (partial?) instance

Someone can correct if I am mistaken... I think knowing when to use each
just depends on the need and convenient.

and when you should use each of them. Is there a clear explanation
somewhere? I would like to use the most succinct method(s) possible, of
course.

Thanks, and Long, Max and Ryan, you are my heroes!

Hmm, I think Max and Ryan really deserve more credit.

Go and enjoy your time off!

Cheers,

Long
www.edgesoft.ca

I responded earlier, but realized that my response may not have been
quite detailed/nitpicky enough. So I'm going to nitpick your
response, if that's okay :slight_smile:

> I'm not quite clear on the differences between
> @payment = Payment.new
Gives you an uninitialized instance

Gives you an instance with all its attributes initialized to default value.

> @payment = Payment.new(params[:payment])
Gives you a new and initialized (partial?) instance

Shortcut for Payment.new and Payment.attributes=. What I mean is that
you'll get an instance with attributes initialized to their default
values, and then any attributes in the hash will be assigned. So for
example, with a Person model that has name and age attributes..

hash = { :name => "John", :age => 30 }
Person.new(hash) # :name => John, :age => 30

hash = { :name => "John" }
Person.new(hash) # :name => John, :age => nil

If you have defaults specified in your db, they'd be initialized to
that instead of nil.

hth

Pat