Rails date/year field - ruby-on-rails

I want to input only year field in my rails form. I tried select_year but it doesn't work for me. select_date also gave me problem because my form models is in association. Is there any alternative idea.

Ref:- select_year
# Generates a select field for years that defaults to the current year that
# is named 'birth' rather than 'year'
<%= f.select_year(Date.today, :field_name => 'birth') %>
but if you don't have any column then just use
<%= select_year(Date.today) %>
Ref this and try something like
<%= select_year Date.today, :start_year => Time.now.year, :end_year => Time.now.year - 95, :field_name => :grad_year, :prefix => :profile %>

Related

include_blank not working for select_year rails form input Rails 6

For some odd reason, the year is still showing as the default value for the select_year input that rails offers. I cannot get include_blank: "Select Year" to populate the form input. How can I force the select_year/select_month input to show the include_blank text instead of the current year/current day value? fyi: prompt also doesn't work for the input
_calendar_form.html.erb
<%= form_tag calendar_path(#calendar), method: :get do %>
<%= select_year(Date.today, {:prompt => "Select Year", :start_year => DateTime.now.year, :end_year => DateTime.now.year - 8, prefix: 'select'}, {:field_name => 'year', :id => 'start-year', class: 'ui dropdown', include_blank: 'Select Year'}) %>
<% end %>
It's because your first argument is the value it should set, as in Date.today
You can set that to nil to get it to work. You can also just skip the include_blank since you also have prompt
<%= select_year nil, {prompt: "Select Year", start_year: DateTime.now.year, end_year: 8.years.ago.year, prefix: 'select'}, {field_name: 'year', id: 'start-year', class: 'ui dropdown', include_blank: 'Select Year'} %>
Usually you will set this in a controller variable
def new
#date = nil
end
def create
#date = some_params[:year] # depending on use-case obviously
# if you need to re-render the form the value is retained
end
= select_year #date ......

Rails Make Form Text Field Show Value From Model or Default Value

Rails 5.0.4 ruby 2.5.1p57
I have a form partial that has a date field. It uses the datepicker class.
Currently, I have it set to display the current-date:
<%= form_for(#weight) do |f| %>
<%= f.text_field :workout_date, { :size => 20, :type => 'date', :class => "date-picker", :readonly => false, :value => Date.today } %>
Because I use this form/partial on both the new and edit views, I would like it to show either the date in the model (for an edit) or the current date (for a new record).
I tried this:
<%= f.text_field :workout_date, { :size => 20, :type => 'date', :class => "date-picker", :readonly => false, :value => #weight.workout_date.present? ? #weight.workout_date : Date.today } %>
But, this displays 09/01/2019, when viewing the new form today 09/12/2019.
Ideas on what I'm doing wrong? Can this be done?
Thanks for any tips.
I think I figured this out. In my migration to create the weights model:
t.date "workout_date", :default => Date.today I created this on 09/01/2019, so the form is using this date.
<%= form_for(#weight) do |f| %> I'm passing a weight object for a new record. It must see that default date and use it.
Fix: create a migration to removed that default date. OR, since the new model record does not have an ID, I can do this cheap-hack, which works, until I fix the model:
<%= f.text_field :workout_date, { :size => 20, :type => 'date', :class => "date-picker", :readonly => false, :value => #weight.id.present? ? #weight.workout_date : Date.today } %>
I also do not need the .present?. It works without it.

Create a Year Dropdown Box FormBuilder In Ruby On Rails

I am trying to create a db that has a year attribute but I am having some difficulty. I created the scaffold and tryied to modify the _form.html.erb with this code:
<%= f.date_select :year, :start_year=>2000, :end_year=>Time.now.year %>
Tried to run the rails server and gave me this error:
1 error(s) on assignment of multiparameter attributes
I realized that I only wanted the year and not the day or month. Is there a way to do that? I tried :discard_month=>true but that just hides it, but still storing it.
Thanks in advance
Do something like:
<%= f.date_select :year, :order => [:year], :start_year => 2000, :end_year => Time.now.year, :prompt => {:year => "Select year"} %>
Because your year field is an int and not a datetime:
<%= f.select :year, (2000..Time.now.year).to_a, :include_blank => {:year => "Select year"} %>
Another alternative is select_year
e.g.,
select_year(Date.current, start_year: 2015, end_year: Date.current.year)

Rails 3 date_select for year only

I have a form in my Rails 3 app where I want to create a select tag for just the year on a method :grad_year. I have everything working - and storing - properly using date_select and adding :discard_month and :discard_day. However when I render #profile.grad_year I get the month and day values. So I'm wondering how to store and render only the year for #profile.grad_year?
Here is the form:
<%= f.date_select :grad_year, {:start_year => Time.now.year, :end_year => Time.now.year - 95, :discard_day => true, :discard_month => true}, :selected => #profile.grad_year %>
In my migration:
t.date :grad_year
Rails has a select_year helper:
http://apidock.com/rails/ActionView/Helpers/DateHelper/select_year
So your code should look like:
f.select_year(Date.today, :start_year => Time.now.year, :end_year => Time.now.year - 95, :field_name => 'grad_year')
Assembling all of the above from #alex_peattie's answer, I arrived at the following:
<%= select_year Date.today, :start_year => Time.now.year, :end_year => Time.now.year - 95, :field_name => :grad_year, :prefix => :profile %>
As with the OP's question, my case was done within a form_for block, so f.select_year threw an exception. But if you just use the documented :field_name option, the tag will have the id date_grad_year and name date[grad_year] which are not what Rails expects. Using the (documented only at the very top of the API) :prefix option changes date to profile.
So this is better than the ##%$^*& html_options hash, which, despite using rails for 5 years now, I cannot seem to get right without five tries :-).
Oh Rails, how I love you, yet at the same time am sure glad Stack Overflow is around to help us all understand your delightful idiosyncrasies!
This select_year function is totally screwy.
Here is finally what works:
<%= form_for(#user) do |f| %>
<%= select_year current_user.birth_year, { :prompt => "Year",
:start_year => Time.zone.now.year - 13,
:end_year => Time.zone.now.year - 80,
:field_name => :birth_year,
:prefix => :user },
class:"form-control" %>
<% ... %>
it aught to be like this in rails:
<%= f.select_year :birth_year, { :prompt => "Year",
:start_year => Time.zone.now.year - 13,
:end_year => Time.zone.now.year - 80},
class:"form-control" %>

Use Rails select_month and select_year to capture and store dates

I would like to use two picklists to control entry into a date field; I'm concerned with the month and year, not the day. So far, I have the following:
<tr>
<td>Date Range:</td>
<td>
<%= select_month(Date.today, :field_name=>"dateStarted") %>
<%= select_year(Date.today, :field_name=>"dateStarted") %>
to
<%= select_month(Date.today, :field_name=>"dateEnded") %>
<%= select_year(Date.today, :field_name=>"dateEnded") %>
</td>
</tr>
This will correctly set the month and year corresponding to today's date. Unfortunately, the select fields do not respond to values in the respective fields. What am I missing?
Moreover, I assume that I will need to combine these two fields in the controller's create and update actions to create an actual date that will be stored in the db, but I am unclear how to do so.
Help is appreciated.
Why not just use date_select (rather than select_month and select_year)
date_select("dateRange","dateStarted", {:default => Date.today, :discard_day => true})
date_select("dateRange","dateEnded", {:default => Date.today, :discard_day => true})
This will create parameters dateRange[dateStarted] and dateRange[dateEnded] with sub-selects for year and month.
One other clarification, to make it work seamlessly with the create and update action, you should replace "dateRange" with the actual model name. That way it will be properly embedded in the parameter hash
If you are using simple form in your rails application, then you can do something like:
= simple_form_for(resource) do |f|
// ...
= f.input :date_started, discard_day: true, end_year: Date.today.year, order: [:month, :year]
= f.input :date_ended, discard_day: true, end_year: Date.today.year, order: [:month, :year]

Resources