my code looks like this
<div class="field form-group">
<%= f.label :date_of_birth %><br />
<%= date_select :date_of_birth, {order: [:month, :day, :year],
prompt: { day: 'Select day', month: 'Select month', year: 'Select year' },
start_year:1950, end_year: Date.today.year}, {required: true}, class: "form-control" %>
</div>
And result is in this photo
What i want that year selection be from 1950 to now.
Thanks for help!
The following code works perfectly for me:
<%= f.date_select( :date_of_birth, { :order => [:month, :day, :year], :start_year => 1950, :end_year => Date.today.year }, { :required => true, :class => "form-control" }) %>
Note that my date_select control is prepended with f, for the form it belongs to... I think that might be your problem.
Related
Here is code for date selector in my rails form-
<div class="field columns large-3">
<%= form.label :planned_start_date, :class=>"required" %>
<%= form.date_select :planned_start_date, start_year: #project_start_year, end_year: #project_end_year, :include_blank => true, order: [:day, :month, :year], class: 'select-date' %>
</div>
<div class="field columns large-3">
<%= form.label :planned_end_date, :class=>"required" %>
<%= form.date_select :planned_end_date, start_year: #project_start_year, end_year: #project_end_year, :include_blank => true, order: [:day, :month, :year], class: 'select-date' %>
</div>
</div>
I want to add dd mm yyyy instead of null selector by default. How can i change that?
As mentioned in ruby doc for date_select
= form.date_select :planned_end_date, start_year: #project_start_year, end_year: #project_end_year, :include_blank => true, order: [:day, :month, :year], prompt: { day: 'dd', month: 'mm', year: 'yyyy' }, class: 'select-date'
I have a date_select:
<%= f.date_select :date_of_birth, {
:with_css_classes => true,
:order => [:month, :day, :year],
:prompt => true,
start_year: Date.today.year, end_year: Date.today.year - 100}, {:class => "select"} %>
When there are errors, and the page re-renders, the value are lost. With a regular select I set selected with the params, but I don't know how or it's possible with a date_select
Ref this, use selected
<%= f.date_select :date_of_birth, {
with_css_classes: true,
order: [:month, :day, :year],
selected: #date
prompt: true,
start_year: Date.today.year, end_year: Date.today.year - 100},
{:class => "select"} %>
Hello I am trying to make a date_select and the class wont take effect. Thanks for all the help.
<%= f.date_select :created_at,
:order => [:month, :day, :year],
:prompt => {month: 'Birth Month', day: 'Birth Day', year: 'Birth Year'},
:start_year => Time.now.year,
:end_year => 1920,
:class => 'SelectDropDown' %>
the form helper that I like is called simple_form. When I need to add a date picker I have been using bootstrap3-datetimepicker-rails
Setup:
File #app/assets/javascripts/application.js
$(document).ready(function() {
$('.datetimepicker').datetimepicker({});
return $('.datepicker').datetimepicker({
format: 'YYYY/MM/DD'
});
});
});
Now to apply it to a form
= f.input :date_select, as: :string, input_html: { class: 'datetimepicker', value: "#{f.created_at}" }
I hope that this helps
try to use html_options for class:
<%= f.date_select :created_at,
order: [:month, :day, :year],
prompt: {month: 'Birth Month', day: 'Birth Day', year: 'Birth Year'},
start_year: Time.now.year,
end_year: 1920,
html_options: {class: "SelectDropDown"} %>
for more details check data_select
i m using the rails date_select and time_select with the following options.
but for date_select start_year and end_year and order options are not working. and for time_select ampm:true option is not working.
<%= label_tag :date,"Date" %>
<%= date_select :date, { start_year: Date.today.year - 1, :end_year => Date.today.year, order: [:day, :month, :year] } %>
<%= label_tag :check_in,"Check In Time" %>
<%= time_select :check_in, {ampm: true} , {} %>
<%= label_tag :check_out,"Check Out Time" %>
<%= time_select :check_out, {ampm: true}, {} %>
and how to apply class attribute. iam giving like this but styling is not working.
<%= time_select :check_out, {ampm: true} ,{:class => 'form-control' } %>
on Rails API docs the date_select is defined as
date_select(object_name, method, options = {}, html_options = {})
you are placing options as the second parameter when it should be the third one.
solution 1:
<%= date_select :date, nil, { start_year: Date.today.year - 1, :end_year => Date.today.year, order: [:day, :month, :year] } %>
You have the same problem for the time_select:
time_select(object_name, method, options = {}, html_options = {})
solution 2:
<%= time_select :check_out, nil, {ampm: true} ,{:class => 'form-control' } %>
The parameters are in the wrong order. It should be:
<%= label_tag :date,"Date" %>
<%= date_select :date, {}, { start_year: Date.today.year - 1, :end_year => Date.today.year, order: [:day, :month, :year] } %>
<%= label_tag :check_in,"Check In Time" %>
<%= time_select :check_in, {}, {ampm: true} %>
<%= label_tag :check_out,"Check Out Time" %>
<%= time_select :check_out, {}, {ampm: true} %>
See also: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select
I have the typical rails _form generated by scaffold for a member model. For the param :state I am using a select tag:
<%= f.select :state, options_for_select(us_states), { :include_blank=>true, :prompt => 'State' }, { :class => 'form-control' } %>
Using a helper method (us states):
def us_states
[
['AK', 'AK'],
['AL', 'AL'], #etc
And for the param "member_since" I am using the select_year helper:
<%= select_year(0, {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
Now both of these selects work to create a new record, but neither field is pre-filled in the edit record view. Any thoughts?
<%= f.select :state, us_states, { :include_blank=>true, :prompt => 'State' }, { :class => 'form-control' } %>
and
<%= select_year(f.object.member_since, {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
UPDATE:
Since member_since is a string, you will need to convert it to date:
<%= select_year(Date.new(f.object.member_since.to_i), {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>