Rails Save Date Range to Database -
i have form field posts date range in format "05/14/2013 - 05/22/2013". model has 2 separate date fields, begin
, end
respective beginning , end dates form field. best mvc way approach getting date range correct fields?
i've been trying manually deconstruct date range in controller's create
method, looks updated params aren't seen in model before record created.
edit: date range coming in format because i'm using keith wood's datepick, , outputs dates in single input field.
what i've been trying (contract
name of model, , dates
input date range:
begindate = params[:dates].split("-")[0].strip()
enddate = params[:dates].split("-")[1].strip()
params.delete :dates
params[:contract][:begin] = date.strptime(begindate, '%m/%d/%y')
params[:contract][:end] = date.strptime(enddate, '%m/%d/%y')
@contract = contract.new(params[:contract])
... these changes params don't show time record created , validated.
define setter on model takes field, splits it, , puts each part appropriate field.
def date_range=(val) begin_str, end_str = val.split(' - ') self.begin_at = date.parse(begin_str) self.end_at = date.parse(end_str) end
this work when called directly, or mass assignment method such update_attributes
or create
. make sure add date_range
(or relevant param name) attr_accessible
if have defined in model.
i've changed field names in example, asbegin
, end
should avoided field names, since 1 method , other part of ruby syntax.
Comments
Post a Comment