Skip to content

Instantly share code, notes, and snippets.

@revskill10
Created June 3, 2013 07:46
Show Gist options
  • Save revskill10/5696651 to your computer and use it in GitHub Desktop.
Save revskill10/5696651 to your computer and use it in GitHub Desktop.
rails, select2 and ajax
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require select2
//= require ckeditor-jquery
//= require_tree .
$(document).ready(function() {
$('#test').select2({
minimumInputLength: 3,
placeholder: 'Search',
ajax: {
url: "/search",
dataType: 'json',
quietMillis: 100,
data: function(term, page) {
return {
term: term
};
},
results: function(data, page ) {
return { results: data.users }
}
},
formatResult: function(user) {
return "<div class='select2-user-result'>" + user.hodem + " " + user.ten + "</div>";
},
formatSelection: function(user) {
return user.hodem + " " + user.ten;
},
initSelection : function (element, callback) {
var elementText = $(element).attr('data-init-text');
callback({"term":elementText});
}
});
});
require "xmlrpc/client"
class HomeController < ApplicationController
before_filter :init_client
def all
result = @client.call("sample.sumAndDifference", "*:*")
render json: {:users => JSON.parse(result) }
end
def search
# Make an object to represent the XML-RPC server.
param = params[:term] || ""
# Call the remote server and get our result
result = @client.call("sample.sumAndDifference", "*#{param}*")
render json: {:users => JSON.parse(result) }
end
def index
end
private
def init_client
@client = XMLRPC::Client.new( "localhost", "/", 8080)
end
end
<input type='hidden' value="192" data-init-text='Bla bla' name='input' id='test' style="width:300px;"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment