Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/01/2014, 09:43
Avatar de calichecal
calichecal
 
Fecha de Ingreso: junio-2009
Ubicación: Colombia
Mensajes: 288
Antigüedad: 14 años, 10 meses
Puntos: 12
Problema con función javascript

Hola a todos, un saludo.

Tengo un inconveniente con un proyecto rails, mas exactamente con una función javascript. Estoy tratando de terminar unos selects dinamicos que ayuden a seleccionar un municipio.

Osea, que se carguen unos municipios en un select en base al departamento seleccionado en otro select:

con este script:

dynamic_municipios.js.erb

Cita:
var municipios = new Array();
<% for municipio in @municipios -%>
municipios.push(new Array(<%= municipio.departamentos_id %>, '<%=h municipio.nombre %>', <%= municipio.id %>));
<% end -%>

function departamentoSelected() {
departamentos_id = $('municipios_departamentos_id').getValue();
options = $('negocios_municipios_id').options;

options.length = 1;
municipios.each(function(municipio) {
if (municipio[0] == departamentos_id) {
options[options.length] = new Option(municipio[1], municipio[2]);
}
});
if (options.length == 1) {
$('state_field').hide();
} else {
$('state_field').show();
}
}

document.observe('dom:loaded', function() {
departamentoSelected();
$('municipios_departamentos_id').observe('change', departamentoSelected);
});

Todo iba bien, estoy siguiendo este tutorial:
http://railscasts.com/episodes/88-dynamic-select-menus


Pero al final cuando lo fui a probar, firebug me arrojo este error:
Cita:
TypeError: document.observe is not a function
[Parar en este error]

document.observe('dom:loaded', function() {
He encontrado una supuesta solución de reemplazar jquery por prototype, pero no puedo hacer eso, estoy utilizando bootstrat.

Agradezco sus aportes y consejos, este es mi gemfile:

Código HTML:
source 'https://rubygems.org'

gem 'rails', '3.2.13'
gem 'strong_parameters'
gem 'mysql2'
gem 'execjs','2.0.2'
gem 'rake' , '10.1.1'
gem 'therubyracer'
gem "less-rails"


# gem 'will_paginate', '3.0.3'
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'

  # ejecutar rails g bootstrap:install, despues del bundle install

end

  gem 'twitter-bootstrap-rails'
  gem 'jquery-rails'
  gem "paperclip", :git => "http://github.com/thoughtbot/paperclip.git"
Y esto es lo que tengo en mi aplication.js

Cita:
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require_tree .
__________________
Hay una fuerza motriz más poderosa que el vapor, la electricidad y la energía atómica: la voluntad. -Einstein-