I am trying to use select2 with my rails application but cannot get the result displayed in my views. I see the correct json coming in chrome console but cannot get the result displayed in the dropdown ...
Could you please help me ?
Thanks :
Controllers :
def friends
if params[:term]
@users = User.where{ ( (first_name =~ my{"%#{params[:term]}%"}) | (last_name =~ my{"%#{params[:term]}%"}) | ( ((first_name.op('||', ' ')).op('||',last_name)) =~ my{"%#{params[:term]}%"}) | ( ((last_name.op('||', ' ')).op('||',first_name)) =~ my{"%#{params[:term]}%"}) ) & ( adult==false ) }
end
respond_to do |format|
format.html
format.json { render :json =>{:results => @users.to_json(only: [:id, :first_name, :last_name]) }}
end
end
My Javascript :
$("#teen_search").select2({
placeholder: "Search for a teen",
minimumInputLength: 2,
ajax: {
url: "/friends.json",
dataType:'json',
data: function (search, page) {
return {
term: search, // search term
page_limit: 10,
};
},
results: function (data, page) {
return data;
}
},
dropdownCssClass: "bigdrop"
});
I cannot figure what's wrong, thanks for your help
I just edited the controller render_to json and the results in js to fix an error in the chrome console, I know I am close but still cannot get my user first_name and last_name displayed in the field ...