根据其他选择过滤集合选择

时间:2017-11-11 14:10:06

标签: ruby-on-rails

我有一个州和另一个城市集合的集合。所以在我的应用程序中,我想根据状态的选择过滤城市的结果。

模型中的代码:

class State < ApplicationRecord
  has_many :cities
end

class City < ApplicationRecord
    belongs_to :state
end

视图中的代码:

<div class="form-group">
    <%= f.label :state, "State:", class: "col-sm-2 control-label" %>
    <div class="col-sm-6">
      <%= f.collection_select :state_id, @cities, :id, :name, {} , class: "form-control" %>
    </div>
  </div>

  <div class="form-group">
    <%= f.label :city_id, "City:",class: "col-sm-2 control-label" %>
    <div class="col-sm-6">
     <%= f.collection_select :city_id, @states, :id, :name, {} , class: "form-control" %>
    </div>
  </div>

有一种方法是,当选择州时,城市会针对属于所选州的城市进行更新吗?

0 个答案:

没有答案
相关问题