Reputation: 9794
all,
Am new to RoR and hence needed some guidance w.r.t Date field.
Background:
1 > I create a model object using rails generate scaffold <Model> name:string remainderDate: date
2 > Now when I have deployed the model to DB using rake db:migrate
and on opening the URL: localhost:3000/<Model>s/new
, the displayed date field is in the format YYYY MM DD, all 3 separate fields with dropdown with
YYYY values >=2006 and <=2016
MM values >= January and <= December (obvious)
DD values >= 1 and <=31
Question 1:
1> Where is the code for setting the values to these fields?
2> Can I change the format to MM DD YYYY?
3> Can I restrict the values being added to the list? i.e. for the current year 2011, only months starting from April should be shown and only dates starting current day should be shown
4> Where can I change the display text for the new
form? I opened up new.html.erb
and could not understand where the display text is set. I also opened up _form.html.erb
and could not locate where necessary changes can be done?
Upvotes: 0
Views: 2307
Reputation: 848
Take a look at the syntax for date_select: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select
In your form you would have something like this:
<%= f.date_select :remainderDate, {:order => [:month, :day, :year],:prompt => true, :start_year => Time.now.year, :end_year => 2016} %>
So answers to your quenstions:
1/ as Wes answered
2/ the :order option in my example
3/ your can only restrict the year, not the months or dates with the options as you see with start_year and end_year in the example. You coud restrict further using validations in your model.
4/ what exactly do you mean with "the display text for the new form"? Do you mean the display text for the date field? That would be next to the <%= f.label %>
Upvotes: 1
Reputation: 6585
Regarding formats of the date field you want to pass the string in the way rails already is because mysql accepts it without modification. That said you can certainly change how the user enters the data on the form for a new record. Look for the view code in
app/views/<model>/_form.html.erb
Upvotes: 1