views:

22

answers:

1

I have a model which has start_at and end_at attributes. In the form for the end-user, I am displaying start_at using the standard datetime_select, but I'd rather not have a second datetime select presented to the user. I'm hoping I can create two fields that represent a duration; one for hours, the other for minutes. My question is, how in my view do I use form helpers to automatically fill in the fields when editing an existing entry. Furthermore, how would I connect that to the model and subsequently save the recording using the real attribute, end_at?

Thanks in advance for any advice you can give!

+1  A: 

I have to do this a bunch and i've been doing the following:

  1. Use the FormTagHelper versions of the calls for the field to be handled specially.
  2. In the controller, read the form_tag values out of the params object.
  3. delete the extra values:
    
    params[:examplemodelname].delete :distance if params[:examplemodelname].has_key? :distance
  1. put the 'real' values into the params object (in your example, ends_at)
  2. call ExampleModelName.new(params[:examplemodelname]) or @examplemodelname.update_attributes(params[:examplemodelname]) as per usual.
corprew
Wouldn't logic like this be better suited for the model? Fat model, skinny controller?
Bloudermilk
My general rule on such things is that messing with the params object should happen in the controller and not the model. Displaying a date as a duration rather than an end value also seems like a view/controller issue rather than a model issue to me.If you cared about this duration in the code in places other than that form, it might be worth putting it the model but for just the use cases stated it seems like a natural for code here or maybe a helper for the form bits.
corprew