Question

I have data coming from sunspot to select2(shown list_styles method of controller). I can search and save multiple categories with select2 on new provider form without any problems however when I try to load the data from database on edit provider form it doesn't show up. Tried the initselection method and checked the documentation/stackoverflow for initselection method that fits to my application but could not sort it out. Created a new controller method called list_categories without success. Can anyone comment me on the correct way to do it? Thank you.

new provider edit provider Jquery

 $('#provider_category').select2({
            minimumInputLength: 3,
            multiple: true,
             ajax: {
              url: "/categories/list_styles",
              dataType: 'json',
              quietMillis: 100,
              data: function (term, page) { 
                return {
                    q: term, 
                    page_limit: 10, 
                    page: page, 
                };
              },
            results: function (data) {
              var hashtable={};
              var results = \[\];
              $.each(data, function(index, item){
               if (hashtable\[item.parent\]===undefined) {
                   hashtable\[item.parent\]={text:item.parent, children:\[\]};
                   results.push(hashtable\[item.parent\]);
               }
               hashtable\[item.parent\].children.push({id:item._id,text:item.title});
            });
              return {
                  results: results
              };
            },
            initSelection: function(element, callback) {
                return $.ajax({
                    type: "get",
                    url: "/categories/list_categories",
                    dataType: 'json',
                     data: function (term, page) { 
                      return {
                          q: term, 
                          page_limit: 10, 
                          page: page, 
                      };
                    },
                    success: function(data){

                    }
                }).done(function(data) { 
                    //console.log(data);
                    return callback(data);
                });

            }
            }
          }); 

Controller class CategoriesController < ApplicationController respond_to :html, :json

  def list_styles 
      search = Category.search do
        fulltext params[:q]
      end
      search = Category.search { keywords params[:q]; paginate :page => params[:page], :per_page => params[:page_limit] }
      @categories = search.results    
      respond_with @categories 
  end 

  def list_categories
      search = Provider.find "5299b5dcdd506322c4000091"
      @category = search.category  
      x = Category.find @category
      search = Category.search { keywords x.title; paginate :page => params[:page], :per_page => params[:page_limit] }
      @categories = search.results 
      respond_with @categories  
  end  
end
Était-ce utile?

La solution

Thats the way it is baby!

jquery

  },
              initSelection : function (element, callback) { 
                var data1 = [];
                jQuery(element.val().split(",")).each(function () {
                    $.ajax({
                        type: "get",
                        url: "/providers/list_categories",
                        async: false,
                        dataType: 'json',
                        data: { id: $("#provider_id").val()},
                        success: function(category){ 
                         $.each(category, function(i, obj) {
                            data1.push({id: this._id, text: this.title}); 
                          });
                        }

                    });
                });
                callback(data1); 
              }

          }); 

contoller

def list_categories
  @provider = Provider.find params[:id]
  arr = @provider.category.split(",")
  @category = Category.where(:_id.in => arr)
  respond_to do |format|
  format.json  { render :json => @category} 
  end            
end 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top