Ruby on rails 添加具有祖先的集合组

Ruby on rails 添加具有祖先的集合组,ruby-on-rails,ruby-on-rails-4,ruby-on-rails-3.2,ruby-on-rails-3.1,ancestry,Ruby On Rails,Ruby On Rails 4,Ruby On Rails 3.2,Ruby On Rails 3.1,Ancestry,Iam与位置模型一起使用。如果用户选择“父项如何显示子项”,则单击“子项”“显示同级项?” 如何在user\u form.html.erb中应用 class Location < ActiveRecord::Base has_ancestry :cache_depth => true has_many :users end 类位置true 有很多:用户 终止 祖先 考虑到祖先为您提供了一系列实现这一点的方法,该过程将相对简单: 您需要做的是在javascript中创建一个o

Iam与位置模型一起使用。如果用户选择“父项如何显示子项”,则单击“子项”“显示同级项?”

如何在user\u form.html.erb中应用

class Location < ActiveRecord::Base
 has_ancestry :cache_depth => true
 has_many :users
end

类位置true
有很多:用户
终止

祖先

考虑到
祖先
为您提供了一系列实现这一点的方法,该过程将相对简单:

您需要做的是在javascript中创建一个
on change
函数,将相对对象传递给控制器,它将返回您需要的结果:

#config/routes.rb
resources :locations do
   get :siblings
end

#app/controllers/locations_controller.rb
Class LocationsController < ApplicationController
   repsond_to :json, only: :siblings

   def siblings
      @location = Location.find params[:id]
      respond_with @location.children #-> might need some logic to differentiate between siblings / children
   end
end
这将为您提供所需的功能

#app/assets/javascripts/application.js
$(document).on("change", "#your_select", function(){
   var id = $(this).val();

   $.ajax({
      url: "/locations/" + id + "/siblings",
      dataTye: "json",
      success: function(data) {
          // append result to your other select boxes
      }      
   });
});