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')]
Related
Im trying to populate a collection with Times like 20.00, 20.10, 20.20 ... 24:00. So in intervals of 10.minutes. But how to do this smartly and take into account the Time.now?
Only times that are > Time.now should be listed.
So if its 20.30 It should not show 20.10, 20.20,20.30
Example code
= f.input :order, :collection => ["20:00","20:10","20:20"... etc ["24:00"],
:default => 2,
:label => "orders,
:hint => "Select the time you want this order to be processed"
Some of the things Ive tried so far:
:collection => [(Time.now + 10.minutes).strftime("%I:%M%p").to_s]
and
#hours=(Time.now.minus_with_coercion(Time.now.midnight)/3600/2)
Any thoughts how to cleanly code this ? Thank you
Not sure to understand your problem but this could help :
Time.parse('20:00').to_datetime.step(Time.parse('23:59'), 10.minutes).to_a.map {|date| date.strftime("%I:%M%p")}
=> ["08:00PM", "08:10PM", "08:20PM", "08:30PM", "08:40PM", "08:50PM", "09:00PM", "09:10PM", "09:20PM", "09:30PM", "09:40PM", "09:50PM", "10:00PM", "10:10PM", "10:20PM", "10:30PM", "10:40PM", "10:50PM", "11:00PM", "11:10PM", "11:20PM", "11:30PM", "11:40PM", "11:50PM"]
After that, you could call the delete_if method to remove unwanted time.
Something like that :
Time.parse('20:00').to_datetime.step(Time.parse('23:59'), 10.minutes).to_a.delete_if {|date| date < DateTime.now.to_time}.map {|date| date.strftime("%I:%M%p")}
My form has:
<%= f.select(:amount, Payment::AMOUNT_VALUES, {:blank => false}) %>
My model has:
AMOUNT_VALUES = { '$ 0' => 0, '$ 1' => 1, '$ 2' => 2, '$ 5' => 5, '$ 10' => 10 }.
If user select $2 and submits form. The next time he goes back to the form, I need it only to display everything above $2 in the select box. Therefore, the options should be $5 and $10 only.
How can this be achieved?
I recently wrote some helper code that helped me rule out days of the week where a store had already posted business hours for. It shouldn't be too hard to adapt to your specifications.
Updated: Added question-specific code and updated with a more-efficient reject block.
module HoursHelper
...
def generateDayStrings (unavailable_days)
all_days = [ ['Sunday', 'Sun'],
['Monday', 'Mon'],
['Tuesday', 'Tue'],
['Wednesday', 'Wed'],
['Thursday', 'Thu'],
['Friday', 'Fri'],
['Saturday', 'Sat'] ]
all_days.reject do |day|
(day - unavailable_days).size != 2
end
end
...
end
<%= f.select :day_of_week, generateDayStrings(#days_with_hours) %>
# #days_with_hours = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
# generateDayStrings(#days_with_hours) = [ ['Sunday', 'Sun'], ['Saturday', 'Sat'] ]
For your particular codebase, it'd probably look something like this:
module PaymentsHelper
...
def generatePaymentAmountSelectArray (unavailable_denoms)
all_denoms = [ ['$ 0', '0'],
['$ 1', '1'],
['$ 2', '2'],
['$ 5', '5'],
['$ 10', '10'] ]
# or all_denoms = Payment::AMOUNT_VALUES
# although not sure how this works with a hash.
all_denoms.reject do |current_denom|
(current_denom - unavailable_denoms).size != 2
end
end
...
end
<%= f.select :amount, generatePaymentAmountSelectArray(#used_values), {:blank => false}) %>
# just make sure you set #used_values in your controller
I feel like there's a better way of doing this, so I'll update if I remember it.
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?
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
I want to paginate posts by month so I added following scope in Post model
class Post
include Mongoid::Document
include Mongoid::Timestamps
scope :by_month, lambda {|end_date| Post.order_by(:created_at => :asc).where(:created_at.gte => (end_date.to_date.beginning_of_month), :created_at.lte => (end_date.to_date))}
end
In my controller I put
def show
#posts = Post.by_month(Time.now).page(params[:page]).per(20)
end
In view
<%= paginate #posts, :theme => 'month_theme' %>
<%= render #posts %>
Problems:
pagination is not working by month, I want to show all result of a month in a page, replacing params[:page] by params[:month]=2 or params[:month]=Feb
How do I view 'August 2011' instead of 1,2
Loop month and year like when you goto previous while in 'Jan 2011' it will goto 'Dec 2010'
I suppose this is not really a matter of pagination. Dealing with the params[:month] value for the query is something different from the page offset switching. You might not need a pagination library for that.
How about simply creating those links like this?
controller:
#posts = Post.by_month(Time.parse(params[:month]) || Time.now)
view:
<% Post.only(:created_at).map {|p| p.created_at.to_date.beginning_of_month}.uniq.sort.each do |m| -%>
<%= link_to_unless_current m, :month => m %>
<% end -%>
Of course you can combine this query with normal pagination if needed. But the pagination links should not be mixed with the month links in that case.