【问题标题】:Add Collection group with ancestry添加具有祖先的集合组
【发布时间】:2014-07-26 22:58:09
【问题描述】:

我将ancestry gem 与位置模型一起使用。如果用户选择父母如何显示孩子,然后点击孩子显示兄弟姐妹?

如何在用户_form.html.erb中申请?

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-3.2 ruby-on-rails-3.1 ancestry


    【解决方案1】:

    祖先

    这个过程会相对简单,考虑到ancestry 为您提供了一系列方法来实现这一点:

    您要做的是在 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
    

    这将允许您定义 ajax 方法来管理来自数据的响应:

    #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
          }      
       });
    });
    

    这应该为您提供所需的功能

    【讨论】:

      猜你喜欢
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多