I'm trying to pass the current date as a hidden field in my form. The problem is that the date gets saved to the database, yet when I try to access it, it is nil.
In my schema:
t.date "spotlight_start_date"
t.date "newsletter_start_date"
t.date "social_media_promotion_start_date"
t.date "personalized_campaign_start_date"
The form: (irrelevant stuff is omitted)
<%= form_for @job do |f| %>
<%= f.hidden_field :spotlight_start_date, value: Time.current %>
<%= f.hidden_field :social_media_promotion_start_date, value: Time.current %>
<%= f.hidden_field :newsletter_start_date, value: Time.current %>
<%= f.hidden_field :personalized_campaign_start_date, value: Time.current %>
<%= f.submit %>
<% end %>
The console: (date attributes have been saved to the database)
...spotlight_start_date: "2016-01-18", newsletter_start_date: "2016-01-18", social_media_promotion_start_date: "2016-01-18", personalized_campaign_start_date: "2016-01-18">
When I try to access the date values I get nil:
@job.spotlight_start_date
=> nil
What's going on? My thoughts are maybe that the hidden fields are saving the date as a string, which is why the rails console thinks that it's nil. I have tried 'Time.now', and putting 'as :date' in the hidden fields with no success.
Please help! Thank you!
In your console type @job.spotlight_start_date.valid?
if false then type @job.spotlight_start_date.errors.full_messages
and it should print what the problem is.