Reputation: 45
I've been trying for a while but I can't seem to access date_select values.
The view looks like this:
<%= form_for(@product) do |form| %>
...
<%= form.label :release_date %>
<%= date_select :@product, :release_date %>
...
When the form is submitted I can see that the values are definitely there as the POST contains:
Parameters: ... "product"=> { "@product"=>{"release_date(1i)"=>"2016", "release_date(2i)"=>"4", "release_date(3i)"=>"13"} ... }
From the answer to a similar question; How to get a Date from date_select or select_date in Rails? it would seem that the correct code to get the value from the key "release_date(1i)" should be something like:
product = params[:product]
date = product["release_date(1i)"].to_i
However the value for date is always 0.
Any advice would be appreciated.
Upvotes: 0
Views: 84
Reputation: 33542
You need to change
<%= date_select :@product, :release_date %>
to
<%= form.date_select :release_date %>
so you will get the ideal params
Parameters: ... "product"=> {"release_date(1i)"=>"2016", "release_date(2i)"=>"4", "release_date(3i)"=>"13"} ... }
and so you can do
product = params[:product]
date = product["release_date(1i)"].to_i
Upvotes: 2
Reputation: 4677
Since you have do |form|
in your form_for tag, you need to bind the release_date
to the form's model like so:
<%= form.date_select :release_date %>
Upvotes: 1