Rails date_select class not working - ruby-on-rails

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

Related

Custom date_select values in Rails?

I would like to change the month select input from date_select : change the values of the array (with January, February... to Janvier, Février...). And I would also like to append styling to each select box (day, month and year). This is my date_select
<%= f.date_select :birthdate,
order: [:day, :month, :year],
prompt: { day: 'Jour', month: 'Mois', year: 'Année' },
start_year: Date.today.year - 13,
end_year: Date.today.year - 100
%>
I don't seem to find anything on the rails official documentation. Any help? Thanks :)
I guess below code would help you.
<%= f.date_select :birthdate,
{
order: [:day, :month, :year],
prompt: { day: 'Jour', month: 'Mois', year: 'Année' },
start_year: Date.today.year - 13,
end_year: Date.today.year - 100,
locale: :fr
},
{class: 'custom-style'}
%>
I guess setting f.date_select ... locale: :fr will help
Use the use_month_names option to set custom values for the months:
<%= f.date_select :birthdate,
{
order: [:day, :month, :year],
prompt: { day: 'Jour', month: 'Mois', year: 'Année' },
start_year: Date.today.year - 13,
end_year: Date.today.year - 100,
use_month_names: ["Janvier", "Février", etc..]
},
{ class: "some-css-class-for-styling" }
%>

How to get select_date to retain selected values on re-render

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

ruby on rails year select issue

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.

prompt not working on date select

I have 2 date fields, date started and date finished. I want to have a prompt for each, as in like on the page's first load, it will display the text "please select".
As you can see the code below, I have included prompt but it is not working.
<%= f.input :year_started, prompt: "Please select", as: :date, label: "Date started", start_year: Date.today.year, end_year: Date.today.year - 100, discard_day: true, order: [:month, :year], include_blank: true, default: nil, :input_html => { :class => 'profile-date' }, :required => false %>
<%= f.input :year_finished, prompt: "Please select", as: :date, label: "Date finished", start_year: Date.today.year + 7, end_year: Date.today.year - 100, discard_day: true, order: [:month, :year], include_blank: true, default: nil, :input_html => { :class => 'profile-date' }, hint: "Or expected graduation month and year.", :required => false %>
Can someone please help me how to get this done in date select. Thanks

Rails select tag in form doesn't populate with value in the edit view

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

Resources