问题
How do I parse values of an rails instance variable to create an auto complete list of jQuery? So my '/user/search' return an instance variable with multiple values per item. I wanna combine them and present as a list item.
def search
if (params[:term] =~ /[a-zA-Z]/)
@result = User.FindLdap("sAMAccountName", params[:term])
else
@result = User.FindLdap("idnumber", params[:term])
end
respond_to do |format|
format.html { render :partial => 'text_for_autocomplete'}
format.js
end
end
I am using jQuery's example code, but it has a way to display each item list as it is. In my case I retrieve multiple ldap attributes and I want to show those attributes values for each list item.
So each list item shall be like ""#{sAMAccountName}", "#{idnumber}""
seperated by a comma, instead of just value of idnumber that was typed in the text field.
$(function() {
$("#term").autocomplete({
source: function (request, response) {
$.post("/users/search", request, response);
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
} #No idea how it can be done here.
});
});
I get this in the list, html of the partial render -

回答1:
First of all you need to look at JBuilder. JBuilder is strong, well-defined and reliable solution for json-output.
来源:https://stackoverflow.com/questions/10392734/ruby-rails-jquery-auto-complete-combine-multiple-values-for-a-list-item