几天来,我一直在挖掘国家和州/省选择的宝石。有些很棒(例如country-state-select)但不适合我的需要。
几乎强制性的country_select gem 很好,但缺少状态。不过是基于这个非常酷的宝石叫做国家。宝石国家确实为 300 多个国家/地区(包括 i18n)汇总了很多很好的信息,比如莫桑比克及其细分市场等等。
在应用程序中拥有国家宝石,所需要的只是一些与之交互的javascript。调用是这样的:
country = ISO3166::Country.new('US')
这是表格:
<%= simple_form_for(@order) do |f| %>
<div><%= f.input :country %></div>
<div><%= f.input :state %></div>
<%= f.button :submit, t('.submit') %>
<% end %>
<script type="text/javascript">
states_drop_down = $("#order_state");
$("#order_country").change(function() {
// How to make this work?
<% unless params[:country].nil? %>
states_drop_down.clearAttributes();
<% ISO3166::Country.new(params[:country]).states.each do |state| %>
states_drop_down.setAttribute(<%= state[:name] %>, <%= state[:alpha2] %>); // How to log to check if new attributes are present?
<% end %>
states_drop_down.reload(); // How to reload the state simple_form input?
<% end %>
});
目标是已知的,每次国家下拉列表更改时,用正确的国家填充国家选择器。有什么帮助吗?谢谢。