1
votes

I'm trying to save a value collected from datepicker (Jquery) to my database. The issue is, it saves correctly sometimes and other times when I change nothing in the code it returns NULL.

CODE

  <%= simple_form_for (@vacay), 
    :url => url_for(:action => 'setVacay', :controller => 'employees'),
    :method => 'post' do |v| %>

<%= v.error_notification %>
<%= v.text_field :dateFrom, :value_method => @vacay %> to <%= v.text_field :dateUntil, :value_method => @vacay %>

Controller

def setVacay
@user = current_user
@vacay = Vacay.new(params[:vacay])

@vacay.save
flash[:success] = "Created vacation request"
redirect_to :controller => "users", :action => "allemploy"
end

IRB Log

1) Parameters: {"utf8"=>"✓", "authenticity_token"=>"b4Uzq7CQvD8qnIBfnr/GmaMCxNjbINxd8CMskmyirPI=", "vacay"=>{"dateFrom"=>"09/20/2011", "dateUntil"=>"09/25/2011", "brought_forward"=>"5", "employee_id"=>"1", "user_id"=>"1"}, "commit"=>"Create Vacay"} User Load (0.4ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1 AREL (0.4ms) INSERT INTO "vacays" ("brought_forward", "dateFrom", "dateUntil", "user_id", "employee_id", "sumVacay", "created_at", "updated_at") VALUES (5, NULL, NULL, 1, 1, NULL, '2011-09-20 01:25:36.804428', '2011-09-20 01:25:36.804428')

2.) When tried at another time. Sometimes it saves params before then doesnt save after or vica versa

AREL (0.4ms) INSERT INTO "vacays" ("brought_forward", "dateFrom", "dateUntil", "user_id", "employee_id", "sumVacay", "created_at", "updated_at") VALUES (5, '2011-01-09', '2011-02-09', 1, 1, NULL, '2011-09-20 01:32:54.097928', '2011-09-20 01:32:54.097928') Redirected to http://localhost:3000/all Completed 302 Found in 336ms

1

1 Answers

0
votes

Could this be a date localization problem? The date 09/20/2011 and 09/25/2011 are clearly in mm/dd/yyyy format but is they are parsed as dd/mm/yyyy format the would be invalid. The might explain why some dates get through and others do not.

In the case that does actually insert values into the database the dates given are valid in either format - 9/2/2011 is a valid value whichever way you interpret either way, although if the dates are getting switched the values inserted into the database won't be as intended. Did you want to book a vacation from 9th January-9th February or 1-2 September?