Getting a field value in a form with ruby

M

Mark Mr

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 :) thanks alot!
 
A

Avdi Grimm

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.
 
C

Craig Demyanovich

[Note: parts of this message were removed to make it a legal post.]

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
 
M

Mark Mr

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.
 
A

Avdi Grimm

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top