Forum: Ruby on Rails Reading date parameters

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.
C8a00e5fbb9b522ce47e448d5f4713ac?d=identicon&s=25 Mat Diss (matdiss)
on 2005-11-30 22:03
Hi

I am pretty new to this, so may be doing this all wrong....

I am having trouble setting the date field in one of my tables from the
data in a form.

I have the following markup:

<label for="payment_date">Date</label><br/>
  <%= date_select "sharedate", "purchase_date", :use_month_numbers =>
false, :order => [:day, :month, :year] %></p>
  <p>

Which generates me the usual date fields on my page.

If want to assign the date to the purchase_date column in my table:

@share = Share.new
@share.purchase_date = @params['sharedate']['purchase_date']

The parameters on my form come back correctly as


"sharedate"=>{"purchase_date(2i)"=>"4", "purchase_date(3i)"=>"30",
"purchase_date(1i)"=>"2005"}

But I get the following error when I try to update the table

Column 'purchase_date' cannot be null: INSERT INTO shares (`cost`,
`quantity`, `purchase_date`, `company_id`) VALUES(1.0, 1, NULL, 2)

Which indicates that the date is not being set.

If I change the code to

@share = Share.new
@share.purchase_date = @params['sharedate']

The date comes out as 0000-00-00

If I try

@share = Share.new(@params['sharedate'])

I get

1 error(s) on assignment of multiparameter attributes

And finally, if I try

@share = Share.new(@params['sharedate']['purchase_date'])

I get the date as NULL again.

I can assign other parameters to columns in the table without problem,
it;s just the date.
I notice that the date parameters are in a strange format with (1i) (2i)
and (3i), so I assume this is something to do with it.

I've now run out of guesses - does anyone have any ideas?

Thanks

Mat.
4098ef08ed2ee3eabeb0ddf0ec03f783?d=identicon&s=25 railsonly (Guest)
on 2005-12-01 01:09
(Received via mailing list)
Hi Mat,

One of the three ways you are doing it should work, that is this way:

@share = Share.new(@params['sharedate'])

The parameters you see:

"sharedate"=>{"purchase_date(2i)"=>"4", "purchase_date(3i)"=>"30",
"purchase_date(1i)"=>"2005"}

Show that the first argument to the Date constructor is the year which
is 2005, the second is the month which is 4, and the third is the month
day
which is 30.  This is a valid date so it shouldn't be causing a
multiparameter attributes assignment error.

Are their any other attributes in the Share model?  Are any of them
possibly
multiparameter?

A way to debug it is to actually look into where it does the
multiparameter
assignment and see what class it thinks it is and for what attribute
it's
having the problem.

Sorry I couldn't be more help,
Frank
C8a00e5fbb9b522ce47e448d5f4713ac?d=identicon&s=25 Mat Diss (matdiss)
on 2005-12-02 21:19
Thanks for taking a look Frank - I'll check it out.

Mat.
This topic is locked and can not be replied to.