Getting a field value in a form with ruby

Hi guys. I have a form to submit an assessment for one of my web apps.
The model contains a datetime field called ends_at which is when the
assessment is supposed to end. Before, I had a datetime_select field for
this, but I put in a calendar popup to do the date for them. However, I
also need them to choose the time as well so this is initially what i
came up with

<%= date_field ‘assessment’, ‘ends_at’, :value => “Click here…” %>
<%= time_select ‘assessment’, ‘ends_at’%>

But unfortunately this replaces the date they selected with today’s date
because it discards the first date field after it reads the time one.
Basically i want to know if there’s a way to somehow combine the date
and the time fields in the form and perhaps pass them in a hidden field
to the controller. I don’t know how to interact with the values inside
fields before they’re submitted. Or if anyone has another suggestion on
how to make that work I’d take that too :slight_smile: thanks alot!

On Mon, Mar 3, 2008 at 4:49 PM, Mark Mr [email protected] wrote:

Or if anyone has another suggestion

I do… ask the Rails mailing list:
http://groups.google.com/group/rubyonrails-talk. This is the Ruby
Language ML.

On your actual problem - why do both fields have to have the same
name? Can’t one be ends_at_date and the other be ends_at_time? And
if they really must have the same name, try adding empty brackets
after the method name: date_field ‘assessment’, ‘ends_at[]’. Then
both values will come back in an array.

Ok I’ll try that google group then. The fields are the same name because
that’s the name of my field in the assessments database. I could split
it up into 2 fields in my database but I was just wondering if there’s a
way to just combine the 2 values so I can put them in that ends_at
field. Thanks though.

Hi,

I agree that you should take this to the Rails mailing list.

Here’s another idea to play with, though.

Give each field a distinct name, e.g., assessment_end_date and
assessment_end_time. When the form is submitted, combine them. Here’s a
rudimentary way it could be done:

assessment.end_at = DateTime.parse("#{params[assessment_end_date]}
#{params[assessment_end_time]}")

Good luck,
Craig

On Mon, Mar 3, 2008 at 5:39 PM, Mark Mr [email protected] wrote:

Ok I’ll try that google group then. The fields are the same name because
that’s the name of my field in the assessments database. I could split
it up into 2 fields in my database but I was just wondering if there’s a
way to just combine the 2 values so I can put them in that ends_at
field. Thanks though.

I suggest you dig into the documentation for the composed_of flag to
ActiveRecord associations. It might give you just the solution you’re
looking for.