1

Datepicker is working for selecting date but it is not saving it to my database. Trying several options as show in code below but it still is not working. This code give me an error. How do I properly format the f.text_field?

           <div class="form-group">
           <label class="control-label col-sm-5" for="time_stop">Job Date:</label>
           <div class="col-sm-7">
             <%= f.text_field :jobdate, :id => @appointment.jobdate, "data-provide" => 'datepicker', @appointment.jobdate, class: "form-control" %>
           </div>
           <script>
  $(function() {
    $( "#jobdate" ).datepicker();
  });
  </script>
        </div>

The error message:

/home/ubuntu/workspace/agss/app/views/appointments/_form.html.erb:95: syntax error, unexpected ',', expecting => ...epicker', @appointment.jobdate, class: "form-control" );@out... ... ^

1
  • The error message: /home/ubuntu/workspace/agss/app/views/appointments/_form.html.erb:95: syntax error, unexpected ',', expecting => ...epicker', @appointment.jobdate, class: "form-control" );@out... ... ^ Commented Jan 15, 2016 at 20:02

1 Answer 1

1

The order of the parameters is wrong. It should be

<%= f.text_field :jobdate, "data-provide" => 'datepicker', class: "form-control" %>

not

<%= f.text_field :jobdate, :id => @appointment.jobdate, "data-provide" => 'datepicker', @appointment.jobdate, class: "form-control" %>

Note that you can't pass a @appointment.jobdate because the value is already taken from the object coupled with the form.

Notice the f.. This is a form builder, not a standard text_field_tag helper.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, I made this change. My field jobdate is still not being saved to database when submitted from _form/html.erb. When I submit the form and it goes to the next display page jobdate is blank. Here is the updated code <div class="form-group"> <label class="control-label col-sm-5" for="time_stop">Job Date:</label> <div class="col-sm-7"> <%= f.text_field :jobdate, "data-provide" => 'datepicker', class: "form-control" %> </div> <script> $(function() { $( "#jobdate" ).datepicker(); }); </script> </div>
You have a problem in your controller then. Open a different question and post the code of your controller.
Also to note, all my other 30 some fields are being saved to the database.
I found a solution. I changed the javascript to <script> $(function() { $( "datepicker" ).datepicker(); }); </script>
Any suggestions on how I can format the date to be mm/dd/yyy? This option does not work: $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }); The problem I have is after the date is saved and I go back to edit I is showing the date as 1901 since it is parsing 2016-01-01.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.