Javascript 用于rails中动态组合框选择的Ajax

Javascript 用于rails中动态组合框选择的Ajax,javascript,jquery,ruby-on-rails,ajax,Javascript,Jquery,Ruby On Rails,Ajax,我有两个组合框在一页。现在我想,如果我选择第一个组合框选项,那么在第二个组合框中,它会自动显示与第一个选择选项相关的选项,而无需页面刷新 我的型号: class Location < ActiveRecord::Base scope :active, -> { where(:is_active => true) } has_many :stores end class Store < ActiveRecord::Base scope :active, -&g

我有两个组合框在一页。现在我想,如果我选择第一个组合框选项,那么在第二个组合框中,它会自动显示与第一个选择选项相关的选项,而无需页面刷新

我的型号:

class Location < ActiveRecord::Base
  scope :active, -> { where(:is_active => true) }
  has_many :stores
end

class Store < ActiveRecord::Base
  scope :active, -> { where(:is_active => true) }
  belongs_to :location
  has_many :companies
end

class Company < ActiveRecord::Base
  belongs_to :store
  scope :active, -> { where(:is_active => true) }
end

我知道使用Ajax是可能的,我也搜索过很多Ajax,但它是针对php的,我不知道如何使用。我以前从未使用过Ajax。请帮帮我。。提前感谢。:)

首先,要了解AJAX与php或rails无关的一点,那就是javascript函数调用

现在根据您在我的HAML视图表中提出的问题

  .field
    .ui-widget
      = f.label :location
      = select_tag(:location, options_from_collection_for_select(Location.active, :id, :name), :prompt => "Select location")

  .field
    .ui-widget
      = f.label :store
      = f.select :store_id, Store.active.collect { |s| [s.name, s.id] }, :prompt => "Select store"
.field
.ui-widget
  = f.label :location
  = select_tag(:location, options_from_collection_for_select(Location.active, :id, :name), :prompt => "Select location", onchange: "change_store(this)")

.field
.ui-widget#store_list
  = f.label :store
  = f.select :store_id, Store.active.collect { |s| [s.name, s.id] }, :prompt => "Select store"
现在在js文件中

function change_store(select_tag){
  value1 = $(select_tag).val()
  $.ajax({
    url: "controller_name/mehuod_name",
    data: {data1: value1}
  })
 }
现在将route放在route.rb文件中,并在各自的控制器中创建方法以查找存储列表,然后您可以使用jquery,simple替换“select_lit”下的html

希望这是你所期待的