bsiddiqui
bsiddiqui

Reputation: 1926

properly storing date info using form_for

I have a form that allows a user to create an event. One of the fields that the user fills out is date. Currently this is the code for the date filed:

<%= u.label :date %> <%= u.text_field :date, :placeholder => 'mm/dd/yyyy' %>

For some reason, if the user types in 01/02/2012 it stores as dd/mm/yyyy, thinking the event is on Feb 1 instead of Jan 2nd. I've already configured my initializer files to display date/datetime the way I would like it to (as recommended on several posts here) but this is still an issue

Update - here is my full form:

    <%= form_for(@party_profile) do |u|%>
    <p>
        <%= u.label :name %><%= u.text_field :name %>
    </p>

    <p>
        <%= u.label :location %><%= u.text_field :location %>
    </p>

    <p>
         <%= u.text_field :date, :placeholder => 'dd/mm/yyyy' %>

    </p>

    <p>
        <%= u.label :password %> <%= u.text_field :password %>
    </p>

    <%= u.submit "Let's Party!", :class => "btn btn-primary" %>
    <% end %>

I have tried replacing u.text_field with select_date as suggested below, but then I get an error:

  undefined method `select_date' for #<ActionView::Helpers::FormBuilder:0x007f9e5e8c69d8>

Upvotes: 2

Views: 7667

Answers (3)

MrHello
MrHello

Reputation: 207

I use that for my dates:

    <%= u.label :date %>
    <%= u.date_field :date %>

Hope it helps =)

Upvotes: 2

bsiddiqui
bsiddiqui

Reputation: 1926

<%= u.label :date %> <%= u.date_select(:date, :order => [:month, :day, :year]) %>

is the best I could come up - still not formatted exactly how I'd like

Upvotes: 6

Andrew Wei
Andrew Wei

Reputation: 2080

Consider using select_date, rather than a simple text field where the user can type in anything.

<%= u.select_date(:date, :order => [:month, :day, :year]) %>

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_date

Upvotes: -1

Related Questions