I have a field in my model, :birth_time, that I'm trying to build from 3 form fields but am having a heck of a time. I've tried two different approaches. Here is a snippet from my form in my first approach:
<input type="text" class="span1" name="birth_time_hour" id="birth_time_hour" placeholder="hour">
<input type="text" class="span1" name="birth_time_minute" id="birth_time_minute" placeholder="minute">
<select name="birth_time_meridiem" id="birth_time_meridiem" class="span1">
<option value="am">AM</option>
<option value="pm">PM</option>
</select>
And my model:
attr_accessible :birth_time
What I want to do in my controller is grab those three form values, create a Time object, and set it to Pick.birth_time before saving. I'm having a problem doing any arithmetic on params["birth_time_hour"]. If it's PM I want to add 12 to it before creating the Time object.
params["birth_time_hour"] + 12
results in can't convert Fixnum into String
params["birth_time_hour"].to_i
results in exception class/object expected
.
Do I need to create an object from the form value first?
My second approach was to add attr_accessible values to the model, even though they aren't actually stored in the database.
attr_accessible :birth_time_hour, :birth_time_minute, :birth_time_meridien, :birth_time
And then in my form (using simple_form):
<%= f.input :birth_time, :label => "Birth time:", :as => :tel, :input_html => { :class => 'span2' }, :placeholder => 'hour' %>
But that just bombs out when trying to render the form:
undefined method `birth_time_hour' for #<Pick:0x0000010657c248>
I'm not really sure why I can't have an attr_accessible for it. I've used it with password fields before that exist only on the form but not in the database.
EDIT: It seems to work better when I use attr_accessible and attr_accessor for birth_time_hour. It can at least build the form but I still don't see it as part of the Pick object in the controller. I have very little knowledge of attr_accessor and what it's used for, that is clear I'm sure.
Any ideas on how to solve a seemingly simple problem? I've read other SO threads that suggest storing all 3 values in the database, which seems stupid as all get out to me. I really hope that's not the case.