How to manipulate date_select to show customized months? Rails - ruby-on-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'%>

Related

bootstrap get crazy date format when editing a date in rails

So, I have a rails application which there is a datepicker. When I put the date for the first time in the calendar it appears in this format: dd-mm-yyyy.
But rails save in the database in this format: yyyy-mm-dd.
When I go to edit this in the form, the datepicker get crazy, it appears a 1900 year, not the actual year.
I know this happens because the format date is different(the date which bootstrap expects and the date which is saved).
I tried change the datepicker format in the edit view(.html.erb) but it doesn't work. How can I solve this?
<%= f.text_field :my_date, "data-provide" => 'datepicker',
'data-date-format' => 'dd-mm-yyyy', class: 'datepicker form-control', :required => true %>

rails date_select, select year only

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.

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 %>

Three text fields for one database entry for rails

I currently have one text field for a date entry, I am trying to split the year, month and day up into three individual entries, seperated by '/'. The original text entry looks like:
<%= f.text_field :date, :placeholder => 'YYYY/MM/DD' %>
I would like to split this into three text_fields, and append them together, and put it into the date entry in the database.
How can I do this?
You often use textfields for dates because you then only needs one field. When you are having three different fields should you consider using select instead. Rails have a the date helper method date_select, so it would be somethink like this:
<%= f.date_select :date %>
This creates one select for years, one for months and one for days.
You can read more on http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select
Add three virtual attributes to the model:
attr_accessor :form_month, :form_day, :form_year
Then put the following in the controller, not the model:
def create
# ...
form_date = [ params[:form][:form_month], \
params[:form][:form_day] ,
params[:form][:form_year] ].join("/")
#my_model.date = Date.parse(form_date)
# ... save and return ...
end
It would be a good idea to manually check each form parameter for validity. Date#parse may spit out the incorrect date if fed an incomplete date string.
Date.parse "2005/11/4"
# => Fri, 04 Nov 2005
Date.parse "/11/4"
# => Mon, 04 Nov 2013
Try to use https://github.com/plataformatec/simple_form
<%= f.input :deadline, :start_year => Date.today.year, :end_year => Date.today.year + 1, :order => [:day, :month, :year] %>
Do you have many options.

Editing Date field - RoR

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

Resources