AutocompleteZipcode
This gem was built to "automagically" fills an address form, for Rails version >= 3.1.
Try it
Installation
Add the following gems to your application's Gemfile:
gem 'autocomplete_zipcode'
And then execute:
$ bundle install
Usage
In app/assets/javascripts/application.js, you should add as follows:
//= require ...
//= require turbolinks
//= require autocomplete_zipcode
Basic Example:
<%= simple_form_for :example do |f| %>
...
<%= f.input :zipcode, as: :zipcode %>
<%= f.input :street, as: :street %>
<%= f.input :neighborhood, as: :neighborhood %>
<%= f.input :city, as: :city %>
<%= f.input :state, as: :state %>
...
<% end %>
If you are not using simple_form, then simply add the data-provider="zipcode"
and the other fields name to the input field yourself.
<%= form_for :example do |f| %>
...
<%= f.text_field :zipcode, data: {provider: :zipcode} %>
<%= f.text_field :street, data: {provider: :street} %>
...
<% end %>
Handle invalid zipcodes
Simply add an event listener callback to zipcode.error
, for example:
document.addEventListener('zipcode.error', function(e) {
alert('Invalid zipcode!!!')
});
Sample projects
For an example, take a look at this repository.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request