rails date_select, select year only - ruby-on-rails

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.

Related

How to manipulate date_select to show customized months? Rails

I need my date select form field to show users only the future dates (like for eg. today is 7 apr 2016, then it must show all dates beyond 7 apr 2016) and for month field it must only show the current month,year(any)
<%= f.date_select :start_date,class: 'form-control'%>
Year: any
Date: only future dates
Month: the current ongoing month
There are two ways:
First is that, you validate the date before saving it, at model level. If date is note in future don't save and return error.
Second, you can use jQuery Date Picker and use its minDate option: http://api.jqueryui.com/datepicker/#option-minDate
Using date_select you only have the option of restricting the starting year but not month and day. http://apidock.com/rails/v3.2.1/ActionView/Helpers/DateHelper/date_select
<%= f.date_select :start_date, start_year: Date.today.year, class: 'form-control'%>

Can I display both (and only) month and year in a date_select helper tag?

I have a form with a date attribute.
Right now I'm using f.date_select :begin_date, discard_day: true but that displays month and year in a each own's select menu respectively.
Is it possible to have both month and year displayed in the same select menu, like below?
2013 January
2013 February
2013 March
2013 April
etc.
It's 7 years later, but this is still is still coming up when I Googled for this exact problem.
You can make it work with a standard ActionView::Helpers::FormBuilder#select and feed it the set of month+years by hand
<% the_months = (Date.new(2020,1,1)..Date.new(2020,12,31)).select { |d| d.day == 1 } %>
<%= f.select :start_date, the_months.map { |d| [d.strftime('%b %Y'), d] } %>
You can have a look to rails API documentation AND date select method for more details.
Your code will be something like this:
<%= f.date_select :date, { :discard_day => true} %>
I had a similar problem where I wanted just one field with a datepicker where the user could only select a month and a year. Today I found out about the month_field FormHelper which solved the problem for me.
I know it's not an answer to the problem the original poster asked about but hopefully it can help others googling for the same problem.
<%= f.month_field :date %>

Rails 3 Formtastic. How can I make formtastic display only the month and year fields WITHOUT the day?

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

Passing a date from a form

I have a form where the user will select a date range, and then a report will be presented based on that date range.
The relevant bit of my Rails form looks like this...
<div class="field">
<b>Start Date</b><br />
<%= date_select :startDate, options = {:order => [:day, :month, :year]} %>
</div>
<div class="field">
<b>End Date</b><br />
<%= date_select :endDate, options = {:order => [:day, :month, :year]} %>
</div>
However, I'm having trouble figuring out how to access the resulting dates in the controller. I've put the following diagnostics in the controller,
puts params[:startDate]
puts params[:endDate]
puts Date.today
...and this shows,
orderdaymonthyear(1i)2011orderdaymonthyear(2i)3orderdaymonthyear(3i)21
orderdaymonthyear(1i)2011orderdaymonthyear(2i)10orderdaymonthyear(3i)21
2011-10-21
My search, is defined at follows,
SalesActivity.find(:all, :conditions => {:created_at => params[:startDate]..params[:endDate]})
...and this gives the exception, 'bad value for range'.
Pointers to how to pass and use a Date range would be appreciated.
Thanks to the helpful suggested answers, I learned that the area I needed to dig into was multiparameter attributes. However, the answer referenced by Julik seems more complex than what I need.
However, I did find this,
http://snippets.dzone.com/posts/show/4630
Which gave me what I was looking for so, my [now working] code looks like this,
#start_date = Date.civil(params[:range][:"startDate(1i)"].to_i,
params[:range][:"startDate(2i)"].to_i,
params[:range][:"startDate(3i)"].to_i)
#end_date = Date.civil(params[:range][:"endDate(1i)"].to_i,
params[:range][:"endDate(2i)"].to_i,
params[:range][:"endDate(3i)"].to_i)
Thanks again for the help.
This answer explains what these parameters are and how they are structured. AFAIK for now Rails does not offer an easy way to add multiparameter assignment support to an arbitrary object (which is what you would want in this case).
Try SalesActivity.all.where("created_at > ? AND created_at < ?", params[:startDate], params[:endDate])
Technically this is not an answer to your specific question, but it is a solution to your problem.
Have you considered using a date select javascript calendar helper? As such in your user interface your user has to select from 6 drop down lists. It may be simpler for them to select 2 from 2 calendar instances. You will then get the value for start date and end date as strings.

Rails -- Can't set value for date_select in form

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.

Resources