动态下拉菜单中的红宝石选择菜单

时间:2013-06-21 15:30:11

标签: javascript ruby-on-rails

我正在尝试实现一个动态下拉菜单,显示状态和相应的城市,在ruby on rails上。

我收到此错误(在/home/yash/socialnw20/app/assets/javascripts/dynamic_cities.js.erb中):

undefined method `each' for nil:NilClass

Javascript文件:

var cities = new Array();

<% for city in @cities do %>
  cities.push(new Array(<%= city.states_id %>, '<%=h city.name %>', <%= city.id %>));
<% end %>

function stateSelected() {
  states.id = $('states.id').getValue();
  options = $('city.id').options;
  options.length = 1;
  cities.ready(function(city) {
    if (city[0] == states.id) {
      options[options.length] = new Option(city[1], city[2]);
    }
  }); 
  if (options.length == 1) {
    $('city.field').hide();
  } 
  else {
    $('city.field').show();
  }
}

document.observe('dom:loaded', function() {
  $('city.field').hide();
  $('states.id').observe('change', stateSelected);
});

0 个答案:

没有答案