At the top of a page I have a javascript datepicker. The selected date is stored in the variable newDate
Lower I have a form in rails like so:
<%= form_for(@checkin) do |f| %>
...
<% end %>
I want the date selected above to get passed as a parameter upon form submission. I want to do something like this:
<%= f.hidden_field :checkedin_at, :value => newDate %>
But I’m pretty sure I can’t mix client-side and server-side code like this. Is there another way to pull this off?
You can create a hidden field like you did, and from javascript you can set the value of that hidden field to the actual value.
Erb part:
Javascript part:
You can do javascript set part when you got the new date value or on submit it’s your decision.
Modifying
If you want to modify an already saved
checkedin_atvariable later, then you need to read it before. So you may need to set the actual value in Erb from the model, and read with:Sidenote
It’s a good idea to do it in an unobtrusive way with javascript. To Be prepared for browsers without javascript support on people with disabilities who cannot use normal browsers.
So a simple HTML solution is a nice to have with visible date field with parsable (not a timestamp) date field.