default value for dropdown collection day of birth field in rails? - ruby-on-rails

rails 3.2.8
simpleform
Im trying to get a default value for each of the day, month, year fields a "Select from list" while keep the below code with styling. Tried nummerous attempts, anyone knows how to do this? Would be very much appreciated thx!
= p.label t(:date_of_birth)
= p.input :dob,
:class => "extra",
:as => :date, :start_year => Date.today.year,
:end_year => Date.today.year - 80,
:month => Date.today.month,
:order => [:day, :month, :year],
:label => false,
:input_html => { :style => "width: 102px" }

I think it is :include_blank => "Select from list"

ugh.. I found out myself after 30 minutes more digging, it was just a simple :prompt => "select" to assign to all 3 dropdowns of the dob collection.

Related

Rails replace f.date_select by html5 input type date

hi together I want to replace this:
<%= raw t("settings.pause", :pause_starts_on => f.date_select(:pause_starts_on, {:include_blank => true, :order => [:month, :day, :year], :start_year => Time.now.year}, {:class => "form-control"}).force_encoding("UTF-8"),
:pause_ends_on => f.date_select(:pause_ends_on, {:include_blank => true, :order => [:month, :day, :year], :start_year => Time.now.year}, {:class => "form-control"}).force_encoding("UTF-8"))%>
by standard html5 input type.
I tried this:
<input type="date" name="pause_starts_on" class="form-control" />
But I got this error:
When assigning attributes, you must pass a hash as an argument.
Whats going wrong?

How to pre select a state in Rails Carmen state_select

What are the options I can pass in a state_select. I am trying to do :selected => state and its not working.
<%= f.state_select :state, 'US', {:prompt => '--Select One--', :selected => state}, :tabindex => autotab, :id => 'state_select, :state => state'%>
I don't think you can use state_select to do it. You'll have to use select with the state_options_for_select function in order to enter your selected state:
<%= f.select :state, state_options_for_select(state_to_select, country_code) %>
That should get you there.

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

nil :selected value for date helper in formtastic

I'm using the following code for a date field:
<%= f.input :date_of_birth, :selected => nil,
:order => [:day, :month, :year],
:prompt => {:day => "Day", :month => "Month", :year => "Year"},
:start_year => Time.now.year - 15,
:end_year => Time.now.year - 100 %>
Everything works as expected, except for :selected => nil. I'm using 0.9.7, all the specs pass, including the one regarding a nil :selected value. When I use this though, the current date is selected, save for the year, which doesn't exist.
Am I missing something here? I tried with :as => :date and that did't make any difference.
Tom
This is a known regression/bug in Formtastic right now. Expect a fix in the next gem release.
You could try to add :include_blank => true, maybe that helps?

Resources