Last Updated: February 25, 2016
· danielpclark

Better Country Selectors in Rails

To search/select for a country in the select dropdown menu.

Install chosen-rails and country_select gem

Include chosen-rails and country_select in Gemfile

gem 'chosen-rails'
gem 'country_select'

Then run bundle install

Include chosen javascript assets

Add to your app/assets/javascripts/application.js if use with jQuery

//= require chosen-jquery

Or with Prototype

//= require chosen-prototype

Include chosen stylesheet assets

Add to your app/assets/stylesheets/application.css

*= require chosen

Enable chosen javascript by specific css class

Add to one coffee script file, like scaffold.js.coffee

$ ->
  # enable chosen js
    allow_single_deselect: true
    no_results_text: 'No results matched'
    width: '280px'

Notice: width option is required since Chosen 0.9.15.

And this file must be included in application.js

//= require chosen-jquery
//= require scaffold

In your form use country_select method as follows

<%= a.country_select :country, ['United States of America'], { include_blank: true }, { class: 'chosen-select' } %>

And that's it. Now you can search/select for a country in the select dropdown menu. ^_^

2 Responses
Add your response


In top one the name 'Gemefile' should be 'Gemfile' ;)

over 1 year ago ·

Thanks! Typo fixed.

over 1 year ago ·