Skip to content

Instantly share code, notes, and snippets.

@hugomaiavieira
Created April 10, 2013 23:24
Show Gist options
  • Save hugomaiavieira/5359331 to your computer and use it in GitHub Desktop.
Save hugomaiavieira/5359331 to your computer and use it in GitHub Desktop.
address.js.erb - javascript for find and fill address fields using brcep gem
// requires
<% routes = Rails.application.routes.url_helpers %>
$(function () {
$('#place_postal_code').on('change', function() {
var $label = $('label[for=place_postal_code]'),
value = $(this).val();
var onSuccess = function(r) {
$('#place_city').val(r.city);
$('#place_state').val(r.state);
$('#place_neighborhood').val(r.neighborhood);
$('#place_street').val(r.street);
$('#place_number').focus();
};
var onFail = function(r) {
response = JSON.parse(r.responseText);
alert(response.message) ;
};
var onComplete = function() { $label.text('CEP') };
$label.text('Pesquisando...');
$.ajax({
url: '<%= routes.find_cep_path %>',
data: { cep: value },
dataType: 'json',
cache: false,
success: onSuccess,
error: onFail,
complete: onComplete
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment