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
$('.chosen-select').chosen
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. ^_^
Written by Daniel P. Clark
Related protips
2 Responses
In top one the name 'Gemefile' should be 'Gemfile' ;)
over 1 year ago
·
Thanks! Typo fixed.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#