Javascript rails 5中的Ajax动态选择下拉列表

Javascript rails 5中的Ajax动态选择下拉列表,javascript,ruby-on-rails,ajax,ruby-on-rails-5,Javascript,Ruby On Rails,Ajax,Ruby On Rails 5,正在尝试按同一表中选定的州从数据库填充美国县。尝试使用这种方法在Rails 5中实现 到目前为止,我能够在浏览器控制台中找到并看到正在进行的呼叫和正确的数据返回,但下拉列表从未得到更新。我试图在不同的路线上混日子,但还是没有运气。请,任何帮助都将不胜感激 #routes get 'account/main/_update_weather', :to => 'client/main#_update_weather', :as => :client_location_java get

正在尝试按同一表中选定的州从数据库填充美国县。尝试使用这种方法在Rails 5中实现

到目前为止,我能够在浏览器控制台中找到并看到正在进行的呼叫和正确的数据返回,但下拉列表从未得到更新。我试图在不同的路线上混日子,但还是没有运气。请,任何帮助都将不胜感激

#routes
get 'account/main/_update_weather', :to => 'client/main#_update_weather', :as 
=> :client_location_java
get 'account/_location', :to => 'client/main#_location', :as => 
:client_location

#controller ../controllers/client/main_controller.rb       
def _location
@weathers = Weather.all
@profile = Profile.new
@county_name = Weather.where("state_code = ?", "AL")
end

def _update_weather
@county_name = Weather.where("state_code = ?", params[:state_code])
respond_to do |format|
format.js
end
end

#Ajax /assets/javascripts/location.js.coffee
 $ ->
 $(document).on 'change', '#state_select', (evt) ->
 $.ajax 'main/_update_weather',
  type: 'GET'
  dataType: 'html'
  data: {
    state_code: $("#state_select option:selected").val()
  }
  success: (data, textStatus, jqXHR) ->
    console.log("Dynamic county select OK!")
  error: (jqXHR, textStatus, errorThrown) ->
    console.log("AJAX Error: #{textStatus}")




#View for update  ../client/main/_update_weather.html.erb
<script>
$("#county_select").empty()
.append("<%=j render(:partial => @county_name) %>") 
</script>

<% @county_name.each do |a| %>
<option value="<%= a.id %>"><%= a.county_name %></option>
<% end %>

#view which trying to update ../client/main/_location.html.erb
<% content_for :head do %>
<%= javascript_include_tag 'location', :media => "all", 'data-turbolinks-
track' => true %>
<% end %>

<%= select_tag "state", options_for_select(state_code) {},
{ id: 'state_select' } %>
<%= f.select :weathers_id, options_for_select(@county_name.collect {|a|
[a.county_name, a.id] }, 0), {}, { id: 'county_select' } %>

您可以将天气更新为HTML。为什么?应该是js。这是一个非常简单的函数,您不需要遵循教程。@Vishal如果我作为数据类型传递:“script”我会得到解析器错误,请尝试传递“dataType:'js”`否则只需不传递数据类型,只需在“main/_update_weather.jsI”这样的路径中添加.js,它仍然会给我解析器错误,但我看到已成功生成响应,其中包含县列表,但它并没有取代country\u select我看不到任何更改$country\u select的代码。您需要添加代码,以HTML形式传递天气更新。为什么?应该是js。这是一个非常简单的函数,您不需要遵循教程。@Vishal如果我作为数据类型传递:“script”我会得到解析器错误,请尝试传递“dataType:'js”`否则只需不传递数据类型,只需在“main/_update_weather.jsI”这样的路径中添加.js,它仍然会给我解析器错误,但我看到已成功生成响应,其中包含县列表,但它并没有取代country\u select我看不到任何更改$country\u select的代码。您需要为此添加代码
Started GET "/account/main/_update_weather?state_code=CA" for 127.0.0.1 at 2017-07-17 22:49:11 -0700
User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 10 ORDER BY `users`.`id` ASC LIMIT 1
Processing by Client::MainController#_update_weather as HTML
Parameters: {"state_code"=>"CA"}
Rendering client/main/_update_weather.html.erb
Weather Load (3.4ms)  SELECT `weathers`.* FROM `weathers` WHERE (state_code = 'CA')
Rendered collection of client/weathers/_weather.js.erb [56 times] (0.7ms)
Rendered client/main/_update_weather.html.erb (8.0ms)
Completed 200 OK in 17ms (Views: 12.0ms | ActiveRecord: 3.4ms)