Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.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
Html 如何根据rails中的国家/地区选择来选择州?_Html_Ruby On Rails_Ruby_Web - Fatal编程技术网

Html 如何根据rails中的国家/地区选择来选择州?

Html 如何根据rails中的国家/地区选择来选择州?,html,ruby-on-rails,ruby,web,Html,Ruby On Rails,Ruby,Web,我在DB、国家和州有两个表。现在,当我基于选择选择选择国家时,它必须显示国家 对于.rhtml文件中的国家/地区选择,我使用的方法如下: <%= select_tag "User[country_id]", options_for_select(Country.all.collect{|x| [x.country,x.id]})%> 现在根据以上选择,我需要选择状态。查看: <%= select_tag :country_select, # name of selectb

我在DB、国家和州有两个表。现在,当我基于选择选择选择国家时,它必须显示国家

对于.rhtml文件中的国家/地区选择,我使用的方法如下:

<%= select_tag "User[country_id]", options_for_select(Country.all.collect{|x| [x.country,x.id]})%>
现在根据以上选择,我需要选择状态。

查看:

<%= select_tag
  :country_select, # name of selectbox
  options_from_collection_for_select(@myrecords, "id", "name"), # your options for this select box
  :'data-remote' => 'true', # important for UJS
  :'data-url' => url_for(:controller => 'MyController', :action => 'getdata'), # we get the data from here!
  :'data-type' => 'json' # tell jQuery to parse the response as JSON!
%>


<%= select_tag
  :state_select, # name of selectbox
  "<option>Please select something from country select!</option>"
%> 
控制器:

class MyController < ApplicationController
  def getdata
    # this contains what has been selected in the first select box
    @data_from_country_select = params[:country_select]

    # we get the data for selectbox 2
    @data_for_state_select = MyModel.where(:some_id => @data_from_country_select).all

    # render an array in JSON containing arrays like:
    # [[:id1, :name1], [:id2, :name2]]
    render :json => @data_for_state_select.map{|c| [c.id, c.name]}
  end
end
#app/controllers/countries_controller.rb
class CountriesController < ApplicationController
   def states
      @country = Country.find params[:country_id]
      @states = @country.states
      respond_to do |format|
         format.json { render json: @states.to_json }
      end
   end
end
Javascript:

$(document).ready(function() {

  // #country_select is the id of our first select box, if the ajax request has been successful,
  // an ajax:success event is triggered.

  $('#country_select').live('ajax:success', function(evt, data, status, xhr) {
    // get second selectbox by its id
    var selectbox2 = $('#state_select');

    // empty it
    selectbox2.empty();

    // we got a JSON array in data, iterate through it

    $.each(data, function(index, value) {
      // append an option
      var opt = $('<option/>');

      // value is an array: [:id, :name]
      opt.attr('value', value[0]);
      // set text
      opt.text(value[1]);
      // append to select
      opt.appendTo(selectbox2);
    });
  });

});

我个人从来没有这样尝试过,在国家下拉列表中尝试这个代码

"f.select_tag :state, State.where(country_id: c.id)"


<div class="field">
   <%= f.label :country %><br>
   <%= f.select :country_id, Country.all.map {|c| [c.name, c.id] }, {prompt:"Choose Country"} %>
</div>
您需要使用ajax获得更新状态,如下所示:

#config/routes.rb
resources :countries do
   get :state, on: :member #-> url.com/countries/:country_id/states/
end
您必须向您的国家/地区控制器添加州操作:

class MyController < ApplicationController
  def getdata
    # this contains what has been selected in the first select box
    @data_from_country_select = params[:country_select]

    # we get the data for selectbox 2
    @data_for_state_select = MyModel.where(:some_id => @data_from_country_select).all

    # render an array in JSON containing arrays like:
    # [[:id1, :name1], [:id2, :name2]]
    render :json => @data_for_state_select.map{|c| [c.id, c.name]}
  end
end
#app/controllers/countries_controller.rb
class CountriesController < ApplicationController
   def states
      @country = Country.find params[:country_id]
      @states = @country.states
      respond_to do |format|
         format.json { render json: @states.to_json }
      end
   end
end
-

您还需要确保表单格式正确

具体来说,您需要使用form_for而不是form_标记:


虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能会无效。感谢您的建议,我将更新答案,使其仅包含基本部分。请继续,并使其有意义。请输入'ajax:success',functionevt,data,status,xhr不工作并引发错误。未根据国家选择检索州数据。使用此选项会导致引导错误增加。