carmen-ruby/carmen

How to display only a few countries

Closed this issue · 17 comments

Hey Jim,

I have been trying to acheive this for a long time but have already wasted a lot of effort. Hoping that you may help me here.

I have the following line in my view:

<%= f.country_select :country, {priority: %w(US CA), prompt: 'Please select a country'}, {:id => "contact_country"} %>

Now, I want to actually just show 2 countries, USA and Canada and nothing else.

How do I limit the number of countries to show. Can you please gimme some example code. I have seen someone saying Carmen.excluded_countries = Carmen.countries - ['US', 'CA']

but I am afraid that is not solving my problem, as I am not sure where to set this. If I do this in initlizer of my application, I am getting error:

undefined method `excluded_countries=' for Carmen:Module (NoMethodError)

Please help me achieving this.

Thanks in advance ..

Hey @sanjusoftware. If you look at the source for #country_select, you'll see that the priority countries are supposed to be provided as a separate argument after the method argument.

I haven't tried this, but I think it'll work:

<%= f.country_select :country, %w(US CA), {prompt: 'Please select a country'}, {:id => "contact_country"} %>

Hi Nick,

Thanks again for the quick reply.

I am sorry if I was not so clear about the problem... The following code works all well.

<%= f.country_select :country, {priority: %w(US CA), prompt: "Please select your country"}, {:id => "project_country"} %>

But what it produces is:

Please Select Your Country
United States
Canada

dotted line

Afganistan
(All other countries follow this list)

But what I actually wanted was that it should just gimme :

Please Select Your Country
United States
Canada

And nothing else after that. Please try the code if you can :)

In that case, don't bother with prioritized countries. Just configure Carmen to exclude every country except for US and CA. I expained how to do that in #91.

Nick,

I tried to use your code.... in #91. But I mentioned in this ticket as well, that I am getting an error while executing your code:

undefined method `excluded_countries=' for Carmen:Module (NoMethodError)

I have your code in a file called countries.rb in the initializer file. Please gimme some sample code. I am just not getting it, I am sorry. I appreciate your help.

Which version are you using? The code that I posted in #91 works with v0.2.13 .

carmen (1.0.0.beta2)
carmen-rails (1.0.0.beta2)

Switch to v0.2.13 , or grep through the source of the version you're using to see if it supports excluding countries.

strange enough : http://www.rubydoc.info/github/jim/carmen/master/frames

I do not see any 0.2.13 here... the latest that I see is : 0.2.8 and if I do gem 'carmen', '0.2.13' and try to "bundle update carmen", it gives me obvious error:

Bundler could not find compatible versions for gem "carmen":
In Gemfile:
carmen-rails (>= 0) ruby depends on
carmen (~> 1.0.0.beta2) ruby

carmen (0.2.13)

Am I missing something here??

What version of carmen and carmen-rails should I be using to get this working...??

I believe carmen-rails is only for use with carmen v1.x .

Try removing carmen-rails from your bundle, and just having carmen v0.2.13 .

OK, so that worked like charm after removing carmen-rails. Thanks a lot Nick.

One little help more if you can. To auto-populate the names of the states, from the demo application of the carmen, http://carmen-rails-demo.herokuapp.com/

it uses a partial subregion_select which has the following line :

<% country = Carmen::Country.coded(parent_region) %>

You can see the code here: https://github.com/jim/carmen-demo-app.

This line is now giving me error, saying that (uninitialized constant Carmen::Country)

Can you tell me how do u get the country for a particular country code in this version of the gem

Thanks for the help, I really appreciate it.

Hey @sanjusoftware. I'm happy to hear that worked.

Your next question doesn't pertain to this GitHub issue. Read through the v0.2.13 docs to find the answer. It's in the README.

jim commented

Hi guys,

Sorry if there haas been confusion regarding the two versions of the library.

sanjusoftware, Carmen::Country only exists in version 1.x. The demo app is for that version.

There is currently a shortcoming in 1.0 that makes it harder to achieve what you're trying to do. I'll be fixing that soon, so you will be able to do something like this:

def only_us_and_canada
  Carmen::Country.all.select{|c| %w{US CA}.include?(c.code)}
end

And then in your form something like this:

  <%= f.select :country, region_options_for_select(only_us_and_canada) %>

I wanted to remove the concepts of excluded countries and priority countries from the base Carmen library, as those are purpose-specific settings and storing them at a global level leads to all kinds of problems. These changes should land in 1.x this week.

jim commented

The API I described above has landed in carmen-rails 1.0.0.beta3. This does require Carmen >= 1.0.0.beta2, though, so please keep that in mind.

I'm going to close this now, feel free to open a new issue as appropriate.

@nickhoffman, thanks for the support help!

You're very welcome, Jim. Thanks for building and maintaining this awesome gem!

@jim and @nickhoffman

Thanks a lot for these changes and support. I would like to thank you both for creating and maintaining this library.

I would like to contribute to this library if there is any help that you need @jim

Thanks again for documenting the
step by step instructions for achieving this objective

Hi,
@jim

I would like to add one county into Carmen::Country . For example i would like to add "Puerto Rico" into country list. Which is the preferable way to add this country?

jim commented

@jithinva007 Take a look at the wiki. The file you want to add is going to be overlay/world.yml.