Using this answer as a guide, I set out to create a select_year that started with today's year and ends 100 years ago. But I wanted to add a :prompt so that the drop down starts with "Year" instead of the current year. So I used the following:
<%= select_year(Date.today, {:prompt => "Year", :start_year => DateTime.now.year, :end_year => DateTime.now.year - 115}, {:field_name => 'Year', :id => 'Date.year'}) %>
This renders a drop down for year, but the prompt shows current year instead of "Year". If I click the drop down though, it shows "Year" as the first option with current year selected. How can I fix this? What did I do wrong?
UPDATE: Here is the HTML output of the above code:
<select field_name="Year" id="Date.year" name="date[year]">
<option value="">Year</option>
<option selected="selected" value="2011">2011</option>
How can I make it so 2011 isn't automatically "selected"?
solution was simple
<%= select_year(0, {:prompt => "Year",
:start_year => DateTime.now.year,
:end_year => DateTime.now.year - 115},
{:field_name => 'Year', :id => 'Date.year'}) %>
I suppopse if default value is out of range you have first of the list selected which is prompt
Related
I,m using bootstrap-datepicker-rails gem for date picker and want to shoe calender only on icon click(not on clicking text filed).
Write Now I'm using in my form:
= form_for (ServiceExp.new), :remote => true do |s|
%ul
%li
= s.label :position
= s.text_field :position
%li
= s.label :start_date
%p
From
%li
.input-append.date.datepicker
= s.text_field :start_date, :class =>"input-mini"
%span.add-on
.calander-icon
To
.input-append.date.datepicker
= s.text_field :end_date, :class => "input-mini"
%span.add-on
.calander-icon
%li
= s.label :description
= s.text_area :description
= s.submit 'Save',:class => "btn btn-primary"
And in javascript:
$('.datepicker').datepicker();
Here, Calendar shows on both text field and icon because class datepicker on top of both. If use this class only on icon than it show calender only on icon click but not adding value on text field. How can I achieve that one. please give any suggestion !!
Add :"data-date-format" => 'dd-mm-yyyy', :'data-date' => '12/2/2013' # default date
To div container containing input box and span.
Like
%li
.input-append.date.datepicker{:"data-date-format" => 'dd-mm-yyyy', :'data-date' => '12/2/2013'} # default date
= s.text_field :start_date, :class =>"input-mini"
%span.add-on
.calander-icon
hopefully this isn't too complicated..
So I have a model with LOTS of attributes, which I've decided to store as indexes in the database which refer to an constant at the model:
class Profile < ActiveRecord::Base
STATUS_CHOICES = %w( single relationship married divorced complicated open )
etc...
In my form, I'm doing this right now:
= f.simple_fields_for :profile do |p|
= dp.input :relationship_status, :required => true, :collection => Datingprofile::STATUS_CHOICES
This displays the collection very well, but of course, the value needs to be set from the index coming through from the model. How would I set the values on the collection to correspond to the correct index of the STATUS_CHOICES array?
Update: Going to re-architect this to make the enum-ed attribs actual AD objects
model
some sort of constant hash:
HASH_NAME = {
0 => "Choose:",
1 => "On-Campus Recruiting - CSO",·
2 => "CSO Staff Referral",
3 => "Faculty Contact",·
4 => "Career Day",·
5 => "CSO Summer Job Listing",·
6 => "Alumni Contact",·
7 => "Personal Contact",·
8 => "Other"·
}
-- view
<%= f.input :some_field, :collection => Model::HASH_NAME.sort.map {|k,v| [v,k]} %>
This would output nice select with select-value as hash key and select-name as hash value, such as:
<select id="form_application_job_source" class="select required" name="form_application[job_source]">
<option value="0">Choose:</option>
<option value="1">On-Campus Recruiting - CSO</option>
<option value="2">CSO Staff Referral</option>
<option value="3">Faculty Contact</option>
<option value="4">Career Day</option>
<option value="5">CSO Summer Job Listing</option>
<option value="6">Alumni Contact</option>
<option selected="selected" value="7">Personal Contact</option>
<option value="8">Other</option>
</select>
Now, if you select say 'On-Campus Recruiting - CSO' from the dropdown, the value that would be stored is : 1
To show it in the view as 'On-Campus Recruiting - CSO' you would have to create a small return function like so in the model itself :
def return_paper_type
HASH_NAME[id]
end
In the view, it would be like this : <%= #instancevariable.return_paper_type %>
One way to quickly solve this would be to make the collection the enumerated indices for the option element values, then use the STATUS_CHOICES array to get the labels using :label_method.
= f.simple_fields_for :profile do |p|
= dp.input :relationship_status, :required => true, :collection => 0..Datingprofile::STATUS_CHOICES.length, :label_method => lambda { |i| Datingprofile::STATUS_CHOICES[i] }
References:
https://github.com/plataformatec/simple_form#collections
https://github.com/plataformatec/simple_form/blob/master/test/inputs/collection_select_input_test.rb#L141
Decided to implement this constants of hashes instead of arrays, which affords the simplicity of one model without the complexity of dealing with indexes..I'll store the hash keys and use the values only in the view.
Whee!!
Add this line to your application's Gemfile:
gem 'enum_help'
And then execute:
$ bundle
In model
class Profile < ActiveRecord::Base
enum relationship_status:{single: 0,relationship:1, married:2, divorced:3, complicated:4, open:5}
etc...
In _form.html.erb using simple_form:
<%= f.input :relationship_status %>
For more information: https://github.com/zmbacker/enum_help
I am using Rails date_select as follows:
<%= date_select :user, :birthday, {:start_year => Time.now.year, :end_year => 1910,:order => [:day,:month,:year], :prompt => { :day => 'day', :month => 'month', :year => 'year' }}, { :class => "default" } %>
When a user forgets to enter a value for the day or month and submit the form when the information is put into the model an error is generated. However the day and month fields also get assigned to 1. This results in the form showing 1 for the day field and January for the month field.
How can I stop this from happening?
select_tag :country_id, options_from_collection_for_select(Country.order('priority desc, name asc'), "id", "name"), { :prompt => 'Select a country', :include_blank => 'None' } %>
Does as expected, except :include_blank => 'None'. Renders an blank option. Like such:
<option value=""></option>
Second, with the select_tag. How do I specify a default value. For example, if I need the select box to select a specific country. I tried adding :selected => Country.first to no avail:
<%= select_tag :country_id, options_from_collection_for_select(Country.order('priority desc, name asc'), "id", "name"), { :prompt => 'Select` a country', :include_blank => 'None', :selected => Country.first } %>
Above always selects "Select a country".
Why?
Blank Value
I don't think this is getting enough attention on other posts:
include_blank on a select_tag does not respect the string passed to it. It only interprets it as a true/false value.
In order to set a blank value for select_tag with a specific string, you need to use prompt.
Selected Value
Because select_tag does not belong to a object, the way select does, you need to specify the selected value as part of the options. pass the selected value into the options param of the select_tag.
In your case, you are using options_from_collection_for_select to help generate those options. This method takes a fourth parameter that specifies which option should be selected.
options_from_collection_for_select(
Country.order('priority desc, name asc'),
:id,
:name,
Country.find_by_name('Canada')
)
I am building a site in rails and I have a date selector with a drop down menu that Rails generate automatically. The problem is that my site is in spanish and the values of the dropdown for the months are in English, is there a way to change the language to spanish?
I tried adding some lines of codes to the config/environment.rb that I found here
The code is basically this:
require 'date'
class Date
MONTHNAMES = [nil] + %w(Enero Febrero Marzo Abril Mayo Junio Julio Agosto Septiembre Octubre Noviembre Diciembre)
module Format
MONTHS = {
'Enero' => 1, 'Febrero' => 2, 'Marzo' => 3, 'Abril' => 4,
'Mayo' => 5, 'Junio' => 6, 'Julio' => 7, 'Agosto' => 8,
'Septiembre'=> 9, 'Octubre' =>10, 'Noviembre' =>11, 'Diciembre'=>12
}
end
end
But nothing changed after I fired up the server again.
I hope you can help me, thanks in advance.
From the documentation:
:use_month_names - Set to an array with 12 month names if you want to customize month names. Note: You can also use Rails’ i18n functionality for this.
So you can either do this:
<%= f.date_select :date, {:use_month_names => ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre','Diciembre']} %>
Or, for bones internationalisation points, replace the strings with t() method calls and use Rails's I18n localisation files:
<%= f.date_select :date, {:use_month_names => [t(:jan), t(:feb), t(:mar), t(:apr), t(:may), t(:jun), t(:jul), t(:aug), t(:sep), t(:oct), t(:nov), t(:dec)]} %>
In config/locales/es.yml
es:
jan: "Enero"
feb: "Febrero"
...
And then in config/application.rb set:
config.i18n.default_locale = :es
Bingo! :-)
In Rails 4:
(polish case:)
= f.datetime_select :start_time, prompt: {day: 'Dzień', month: 'Miesiąc', year: 'Rok'}
pl:
date:
order: ["year", "month", "day"]
month_names: ["Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień"]
it's all
in rails 5 > easiest and best scalable way
in your view:
<%= f.date_select :start_time %>
in your config/locales/en.yml add this :
en:
date:
order: ["day", "year", "month"]
month_names: ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "August", "September", "Oktober", "November", "December"]
Just change the month names in between the strings in whatever you want.
For rails 4.2.5.1 or higher you can use t('locale_parameter_name') instead t(:locale_parameter_name)
E.g:
:use_month_names => [t('jan'), t('feb'), t('mar'), t('apr'), t('may'), t('jun'), t('jul'), t('aug'), t('sep'), t('oct'), t('nov'), t('dec')]