【问题标题】:Rails: City-state gem "f.select" issue to getting data states from selected countryRails:从选定国家/地区获取数据状态的城邦 gem“f.select”问题
【发布时间】:2018-11-14 21:53:54
【问题描述】:

您好,亲爱的所有 Rails 社区。我使用城邦 gem 为国家和州制作下拉菜单。我看过 this tutorialthis github samples 。在示例代码中,它就像 belov;

 <div class="field">
  <!-- Country -->
  <%= f.label :country %>
  <%= selected_tag :country, options_for_select(CS.countries.map { |c| [c[1], c[0]] } ) %>
</div>

<div class="field">
  <!-- State -->
  <%= label_tag :state %>
  <%= selected_tag :state, options_for_select([]) %>
</div>

所以我将“selected_tag”名称更改为“f.selected”

<div class="field">
      <!-- Country -->
      <%= f.label :country %>
      <%= f.select :country, options_for_select(CS.countries.map { |c| [c[1], c[0]] } ) %>
    </div>

    <div class="field">
      <!-- State -->
      <%= label_tag :state %>
      <%= f.select :state, options_for_select([]) %>
    </div>

现在不工作了。

关于问题的详细信息:当您选择任何国家/地区时,它会从国家/地区获取状态。但是当我将名称从“selected_tag”更改为“f.select”时,它现在没有得到状态。它看起来很空

你能检查一下链接并告诉我为什么不工作吗?

谢谢。

【问题讨论】:

  • 除了缩进,它们对我来说看起来一样吗?当您说“不工作”时,会发生什么 - 任何错误或其他行为?
  • 抱歉。当您选择任何国家/地区时,它会从国家/地区获取状态。但是当我将名称从“selected_tag”更改为“f.select”时,它现在没有得到状态。它看起来很空。

标签: ruby-on-rails


【解决方案1】:

当您将字段从 select_tag 更改为 f.select 时,您将更改选择元素的 html id 和名称。 f.select 将生成一个 id 和 name,其前缀为传递给表单的模型。您的 javascript 将寻找像 countrystate 这样的 id。将元素更改为使用 rails form builder f.select 标签后,ID 将类似于 famous_person_countryfamous_person_state

在浏览器中检查您的 html 源代码(查看源代码),以查看在您的表单中为选择标签生成了哪些 ID,并相应地更新您的 cities.js javascript。它应该类似于:

var country = document.getElementById("famous_person_country");
var state = document.getElementById("famous_person_state");

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2011-10-12
    • 2012-03-29
    • 1970-01-01
    相关资源
    最近更新 更多