Zabba
Zabba

Reputation: 65467

How to restrict country names in the `country_select` plugin?

I'm using simple_form, which automatically uses country_select plugin when using a field named country, like this:

<%= f.input :country %>

But I want to be able to restrict the countries displayed.

I saw country_select code defines this:

COUNTRIES = ["Afghanistan"
  ...
"Yemen", "Zambia", "Zimbabwe"] unless const_defined?("COUNTRIES")

So, I though I could override COUNTRIES like below:

<% COUNTRIES = ["Canada","USA"] %>
<p><%= f.input :country %></p>

But I get an error:

compile error
/home/jack/src/beta/app/views/contacts/_address_fields.html.erb:6: dynamic constant assignment
');  COUNTRIES = ["Canada","USA"] 
      ^

How to overwrite the COUNTRIES constant? Or is there a more elegant way of doing this?

Ps. I am using Ruby 1.8.7p330 with Rails 3.0.3

Upvotes: 2

Views: 1724

Answers (1)

jdl
jdl

Reputation: 17790

The COUNTRIES constant is already defined by the plugin by the time your view is executed. Define your COUNTRIES in an intializer. (See: config/initializers)

Edit: Put this in an initializer, like config/initializers/countries.rb:

ActionView::Helpers::FormOptionsHelper::COUNTRIES = ["X", "Y", "Z"]

Upvotes: 4

Related Questions