Created
April 10, 2013 23:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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