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?
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 %>
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
Related
I have a form which allows user to select date, in the view i have it so the user only selects the year, but when the date is saved, it saves todays day and month along with it? all I want it to display is the year, which the user selects, this is my code:
<%= f.date_select :finishdate, :order => [:year] %><br>
If you wish to show only the year value in the form and store it in the database without the month and day, then you can have an integer field and only show the year value as follows:
<%= f.select :finishdate,Date.today.year-10 .. Date.today.year+10 %>
you can try
<%= f.select_year :finishdate, :order => [:year] %><br>
What is the end result that you'd like to work with? If it is only the year, you'll probably not want to use the date_select as that, I believe, will return a DateTime instance and not just the year.
If you do need just the year, you can look into other helpers like select_year.
I develop an application in Ruby on Rails 4 with TimeZone per request.
I want to use a datetime picker (http://xdsoft.net/jqplugins/datetimepicker/) in my application to replace the default Simple_form datetime input (5 combo-boxes...).
For this kind of datetime picker (I search for others, it's the same), in my view, I have to use a "string" field, like this :
<%= f.input :done_at, as: :string, input_html: { :data => { :behaviour => "datetimepicker" } } %>
When I post the form, Rails take care of the timezone and store in the database the time in UTC.
For example, I put "2014-03-14 19:45:07" (local time is Paris, so UTC +0100) in the field, and I have "2014-03-14 18:45:07" in the database, in UTC. It's correct.
But when I want to edit the information, Rails fill in the field with a wrong time. The offset timezone is lost and I have "2014-03-14 18:45:07" in the field (the UTC time), so 1 hour before the correct time.
How can I have the correct time taking care of the user timezone ? (not in UTC)
I tried the solution found on http://jessehouse.com/blog/2013/11/15/working-with-timezones-and-ruby-on-rails/ to override the display of dates, but it doesn't work.
def done_at
super.in_time_zone(time_zone) if super && time_zone
end
If in my view, I put #action.done_at, the time is correct but not in the field.
Thanks,
Fred
Set the value of the input explicitly. You can move #object.done_at.in_time_zone(time_zone) to a helper if you want
<%= f.input :done_at, as: :string, input_html: { :data => { :behaviour => "datetimepicker" }, :value => #object.done_at.in_time_zone(time_zone).strftime('%d-%m-%Y %H:%M') } %>
I have a tool set to store everything in the db as UTC. On a form I have a text field with an attached jQuery Date + Time picker.
In that textfield the dateTime stamp (w/ UTC offset) will just appear like:
<%= f.text_field :report_time, :size => 22 %>
2012-06-05 15:55:50
However I need this form choice to be in EST. So I need to convert it going to the form and then when the form is submitted. This problem is specific only to when an 'EDIT' action is taken (time already exists in field). If I do any addition or subtraction "UTC" gets stuck on the end in the form text field:
#report.report_time = #report.report_time - 5.hours
=>2012-06-05 10:55:50 UTC
At this point a jQuery DateJS dateparse function I'm using will stop working because it doesn't understand the "UTC" text in the text field holding the dateTime.
How can I take a db UTC time and add/subtract hours without "UTC" getting dropped in the form, or how can you remove the UTC offest information leaving only the raw dateTime information?
How do I:
#report.report_time = #report.created_at - 5.hours
=>2012-06-05 10:55:50 UTC
# do something here to strip " UTC" off the dateTime ??
=>2012-06-05 10:55:50
Thank You!
Rails 2.3.5 / Ruby 1.8.7
Use the strftime function to set the value of the field directly, if you don't want the default representation.
<%= f.text_field :report_time, :size => 22, :value =>"...." %>
http://ruby-doc.org/stdlib-1.8.7/libdoc/date/rdoc/DateTime.html#method-i-strftime
You may probably want to check out this link for information on how to use the date/time more naturally with the form builder.
http://guides.rubyonrails.org/form_helpers.html#using-date-and-time-form-helpers
I need formtastic to display only month and year fields, WITHOUT the day fields.
The datepicker is nice but it shows the whole calendar. I don't want the datepicker.
f.input :accounting_month, :label => "Accounting month", :as => :datepicker
All I need is the month and year.
There is a way to do this:
<%= f.input :accounting_month, :label => "Accounting month", :order => [:month, :year]
This will automatically hide the "day" input and give it a default value of 1.
try this
f.input :accounting_month, :as => :date_select, :discard_day => true
It doesn't work that way because without day this implies 28-31 (depending on month) days and it could be any one of them. If you use want to store a month-year selection without day you'll see it is a range of dates, not "a" date.
Advice with dates is to always store the whole date as a date field. If you only know month and year you'll need to have two (non-date) fields, one for each. But as you can see it's going to lose you a lot and you'll need to custom craft each field, validate it, etc. major pain so needs pretty good reason to do it..
The only other thing I can suggest is:
create a hidden div, or apply css if you can't 'get inside' the standard date field on the form, for the 'day'. Then set the value to always be 01. Then you might have a 'date' set of fields that will save 01-month-year to the database. This is definitely a 'hack'!
With the new syntax in Ruby 1.9
f.input :date, as: :date_select, discard_day: true
I'm pulling data from an API where the date data comes in as "2008-02-11 00:00:00 "
I would like that data to go into my form within the date_select as a value so I can view it correctly before I add it into my database.
The view looks like
<%= f.label :start_date %><br />
<%= f.date_select :start_date, :value => " #{#stdate[idx]} " %>
The object is actually an array of dates since I'm doing this action several times do thats why the [idx] is there; serving as an index.
<%= #stdate[idx] %> ends up outputting "2008-02-11 00:00:00 " but the fields for the date_select helper only outputs the current date "2010" "June" "5" in those dropdown date selects fields...
Do I need to set the values of the Year, Month, and Date Individually? I have Chronic and tried to parse the object before using it as a value for the date_select and that didnt work either.
Any ideas?
You wouldn't use the :value option but the :default option and pass a DateTime object to it.
There is no :value option for date_select. In your example, the value of the dropdowns will be obtained from the start_date attribute of whatever object you passed in when you started the form builder f.
On this object, you can simply set the start_date attribute before rendering, even if you're not actually saving it there.
There's also a select_date helper, which is the variant that is not linked to an object, and just allows you to pass a value. But that requires more manual labor, because it doesn't work out of the box with update_attributes.