I have this code
in view
grouped_options_for_select(#grouped_options, nil, #options_1)
in controller
#options_1 = [africa]
#grouped_options = [['North America',[['United States','US'],'Canada']],
['Europe',['Denmark','Germany','France']]]
how to make the countries populate dynamically from an array an not type them one by one?
for those who might be interested...
this what i did
in view
options_for_select(#options, :disabled => ['North America', 'Europe']
in controller
#countries.each do |l|
#options << [l.name, l.id]
end
for this output
Africa
North America
United States, US
Canada
Europe
Denmark
Germany
France
Related
I want to display a list of languages to choose from. For example, if I supposed Japanese and English then the drop down would show two options like so:
日本語
English
in your config/locale_settings.yml add
available:
ja: "日本の"
en: "English"
then in dropdown,
<%= f.select :locale, available_language_options %>
where available_language_options defined as
def available_language_options
options = []
AVAILABLE_LANGUAGES.each do |locale, language|
options << [language, locale]
end
options.sort_by { |o| o[0] }
end
I am using countries gem and country-select gem.
I have an attribute in my phones table called :country_code. I ask users in the new phone form to pick their country:
<%= f.input :country_code, label: false, wrapper: :vertical_input_group do %>
<span class="input-group-addon">Country</span>
<%= country_select("phone", "country_code", {priority_countries["AU", "GB", "NZ"], selected: "AU" }, class: "form-control" ) %>
<% end %>
I want to take that country and get the international prefix.
I have a phones model, which I have:
def landline_number
[international_code, area_code, phone_number].join(' ')
end
def international_code
self.country_code = ISO3166::Country[country_code]
country_code.translations[I18n.locale.to_s] || country_code.international_prefix
end
When I try:
<%= #phone.landline_number %>
It prints the country name instead of the international prefix.
How do I get it to print a number instead of country name?
It seems to me that your problem is this line:
country_code.translations[I18n.locale.to_s] || country_code.international_prefix
according to the documentation examples
# Get a specific translation
c.translation('de') #=> 'Vereinigte Staaten von Amerika'
c.translations['fr'] #=> "États-Unis"
ISO3166::Country.translations # {"DE"=>"Germany",...}
ISO3166::Country.translations('DE') # {"DE"=>"Deutschland",...}
you are actually extracting the country's name first and since it's found the code
country_code.international_prefix
is not executed due to the || condition you have. If you would remove the code to the left of the || it should work. In other words try leaving your method like this:
def international_code
self.country_code = ISO3166::Country[country_code]
country_code.international_prefix
end
Say I have a Users table, and each user has a country_id (the list of countries is stored in another table).
I want to display a list of each country and how many members it has, ie:
Canada: 16
Romania: 12
USA: 9
I was using some raw SQL, but the move to postgres was messy, so I'd like to use a cleaner implementation. Is there a nice 'railsy' way to go about getting said list?
This should return a hash with country_id => count pairs:
#users_by_country = User.group(:country_id).count
#=> { 1 => 104, 2 => 63, ... }
The generated SQL query looks like this:
SELECT COUNT(*) AS count_all, country_id AS country_id FROM `users` GROUP BY country_id
And in your view:
<% #countries.each do |country| %>
<%= country.name %>: <%= #users_by_country[country.id] %>
<% end>
though you have not provided any detail . so its hard to answer exaclty, but may be this will help
User.group(:country_id).count
If you have a Countries model and you want to do it a little differently so that it is more human readable you could use .map.
Countries.map{|o| { Country: o.name, members: o.members.count } }
Obviously you need to have the associations. If countries is just a field on User then this will not work.
BR
What I'm trying to do:
I'm trying to find out if there is a quick way to get country_select to store integers representing the countries instead of storing the country names/strings themselves. I'm using the country select gem.
Possible solution:
One way I'm thinking this can be done is if I install the gem as a plugin and edit the countries array to have arrays within the main array with integers and a string e.g. COUNTRIES = [["United Kingdom", 1],["United States", 2]]
This way in my form there will be values representing the strings. Then I can have a country_id column in my profiles table where users selected countries id will be stored. I will have a separate table "countries" that will have the countries stored in it and I'll use the country_id of the profiles table to reference the correct country in the countries table.
Doing it this way would mean I would still get the good features of the gem/plugin such as having priority countries at the top of the select list. Something I don't know how to do on my own.
It would take long but it could work. If I chose this solution where would I put the plugin? vendors folder in my apps directory right?
But if there is a quicker way to do this I'd like to do it that way.
A bigger picture:
Ok I have a search form where a user can browse the sites users filtering out results by:
text typed location
gender
sexuality
marital status
country
I'm using thinking sphinx and when filtering attributes it seems that the attributes need to be represented integers because everything works but the country.
I'm using the country select gem and it seems to only store strings and not an integer representing the string.
I would like to have it store integers instead.
Here are some contants I use in my search forms:
module ApplicationHelper
GENDER = [["Select", nil],["Male", 1],["Female", 2]]
ETHNICITY = [["Select", nil],['Black', 1 ],['White / Caucasian', 2 ],['European', 3 ],['Asian', 4 ],
['Indian', 5 ],['Middle Eastern', 6 ],['Native American', 7 ],['Hispanic', 8 ],
['Mixed Race', 9 ],['Other Ethnicity', 10 ]]
MARITAL_STATUS = [[' Select', nil],['Single', 1 ],['Dating', 2 ],['In relationship', 3 ],['Married', 4 ],
['Living Together', 5 ],['Divorced', 6 ],['Separated', 7 ],['Widowed', 8 ]]
SEXUAL_PREFERENCE = [[' Select', nil],['Straight', 1 ],['Gay', 2 ],['Bi-sexual', 3 ]]
end
The search/browse form:
<%= form_tag browsers_path, :method => 'get' do %>
<p>
Location: <%= text_field_tag :location, params[:location] %>
<br />
Country: <%= country_select :country, [ "United Kingdom", "France", "Germany" ] %>
<br />
Gender: <%= select_tag :gender, options_for_select(ApplicationHelper::GENDER, params[:gender]) %>
<br />
Ethnicity: <%= select_tag :ethnicity, options_for_select(ApplicationHelper::ETHNICITY, params[:ethnicity]) %>
<br />
Marital status: <%= select_tag :marital_status, options_for_select(ApplicationHelper::MARITAL_STATUS, params[:marital_status]) %>
<br />
Sexual preference: <%= select_tag :sexual_preference, options_for_select(ApplicationHelper::SEXUAL_PREFERENCE, params[:sexual_preference]) %>
<br />
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
as you can see each array has a string and an integer. If you check out the array from the country_select gem there are just strings.
I'd appreciate an explanation of the best possible way to do what I'm trying to do and a clear example if possible.
I hope this post made sense.
Kind regards
I ended up recreating the country list from wikipedia with name of country and iso code as value. Much more straight forward and I got to store countries as their iso code and as integers making it possible to use the attribute with thinking sphinx.
Create the constant like you have for the other filters. Then use a virtual attribute to translate and set the values so that you can store the int version and not the string.
EDIT:
Maybe I do not understand what we are talking about exactly but if you are trying to store the value in a model somewhere you can do something like this:
def country_int(country_s)
country = COUNTRY[country_s]
end
def country_int
COUNTRY.key(country)
end
I have a form for a rails model, where a specific input is saved down as text, and that text is in the drop down in the form.
At the moment, I have the following (using formtastic here):
= f.input :education_level, :as => :select, :collection => ["Some GCSEs or less", "College A levels", "Some University, no degree", "University graduate - Bachelors or equivalent", "Masters degree", "Professional Degree", "Doctorate"], :include_blank => false
Thing is. This doesn't seem to be a great way of doing this. It's loaded up my view with something that should probably be in the model, but that's not very good when it comes to supporting multiple locales.
So, what's the best way of doing this?
This discussion on the rails-i18n google group has an interesting solution:
#in a locale file
en:
education_levels:
gcses: Some GCSEs or less
collegeA: College A levels
some_university: Some University, no degree
bachelors: University graduate - Bachelors or equivalent
masters: Masters degree
professional: Professional Degree
doctorate: Doctorate
#And in a model:
class Education < ActiveRecord::Base
def translated_education_level
I18n.t(education_level, :scope => :education_levels)
end
end
#In a helper:
module EducationHelper
def education_levels
I18n.t(:education_levels).map { |key, value| [ value, key ] }
end
end
#And then in your view:
<%= f.select :education_level, education_levels %>