Skip to content

Instantly share code, notes, and snippets.

@cmilfont
Created October 6, 2009 19:54
Show Gist options
  • Save cmilfont/203354 to your computer and use it in GitHub Desktop.
Save cmilfont/203354 to your computer and use it in GitHub Desktop.
#usando o google como host do jquery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
#código ajax
$(document).ready(function() {
$("#id_estado").change(function() {
$.getJSON("municipios/"+$(this.id)+"/municipios.json",
function(data){
$.each(data, function(i, item){
$("<option/>")
.attr("id", item.id)
.text(item.nome)
.appendTo("#id_municipio");
});
});
});
});
#arquivo routes.rb
map.municipios_de_um_estado 'municipios_por_estado/:uf/municipios.:format',
:controller => "municipios",
:action => "municipios_por_estado"
#controller
class MunicipiosController < ApplicationController
def municipios_por_estado
@municipios = Municipio.find_all_by_uf params[:uf]
respond_to do |format|
format.json {
render :json => @municipios
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment