Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 是否可以只显示所选国家的县?_Ruby On Rails_Ruby On Rails 3_View - Fatal编程技术网

Ruby on rails 是否可以只显示所选国家的县?

Ruby on rails 是否可以只显示所选国家的县?,ruby-on-rails,ruby-on-rails-3,view,Ruby On Rails,Ruby On Rails 3,View,我想将国家和地区显示在单独的选择字段中。 但县同时显示了国家名称和表中的所有县 如何将县的记录约束到属于国家的唯一县,这是在先前的字段选择中选择的 在我的模型中,我有如下模型 用户(此模型有一个用户配置文件) UserProfile(此模型属于用户,具有一个国家和一个地区) 国家/地区(此模型属于UserProfile,有多个州) 地区(此模型属于国家和用户配置文件) \u form.html.erb <%= f.fields_for :user_profile do |profile

我想将国家地区显示在单独的选择字段中。
但县同时显示了国家名称和表中的所有县

如何将县的记录约束到属于国家的唯一县,这是在先前的字段选择中选择的

在我的模型中,我有如下模型

  • 用户(此模型有一个用户配置文件)
  • UserProfile(此模型属于用户,具有一个国家和一个地区)
  • 国家/地区(此模型属于UserProfile,有多个州)
  • 地区(此模型属于国家和用户配置文件)
\u form.html.erb

<%= f.fields_for :user_profile do |profile_form| %>

  <%= profile_form.label :country_id %><br />
  <%= profile_form.collection_select :country_id, Country.order(:name_en), :id, :name_en, include_blank: true %>

  <%= profile_form.label :prefecture_id, "State or Province" %><br />
  <%= profile_form.grouped_collection_select :prefecture_id,Country.order(:name_en),:prefectures, :name_en, :id, :name,include_blank: true %>

<% end %>



型号/国家/地区.rb

<%= f.fields_for :user_profile do |profile_form| %>

  <%= profile_form.label :country_id %><br />
  <%= profile_form.collection_select :country_id, Country.order(:name_en), :id, :name_en, include_blank: true %>

  <%= profile_form.label :prefecture_id, "State or Province" %><br />
  <%= profile_form.grouped_collection_select :prefecture_id,Country.order(:name_en),:prefectures, :name_en, :id, :name,include_blank: true %>

<% end %>
有很多:县

模型/县。rb

<%= f.fields_for :user_profile do |profile_form| %>

  <%= profile_form.label :country_id %><br />
  <%= profile_form.collection_select :country_id, Country.order(:name_en), :id, :name_en, include_blank: true %>

  <%= profile_form.label :prefecture_id, "State or Province" %><br />
  <%= profile_form.grouped_collection_select :prefecture_id,Country.order(:name_en),:prefectures, :name_en, :id, :name,include_blank: true %>

<% end %>
属于:国家


您必须在国家发生变化时触发ajax请求,该请求将根据国家获取地区列表


您可以参考帮助您实现这一目标的源代码。

您可以使用country查看辖区