This article summarizes the application implementation of the so-called chat-space that appears in a certain programming school. When I graduated from school, I remembered that there was an application implementation, so I wrote an article.
Immediately, what an application implementation is is to make sure that you don't even search for users you've added to a group.
What is the flow of operation of this function?
If the above is described in the source code, it will be as follows (only the changed part is displayed).
let input = $("#user-search-field").val();
let ids = [];
$.each( $(".chat-group-user").find("input"), function(key,value){
ids.push( $(value).attr("value") );
})
$.ajax({
type: "GET",
url: "/users",
data: { keyword: input,ids: ids },
dataType: "json"
})
def index
@users = User.search(params[:keyword], params[:ids])
respond_to do |format|
format.html
format.json
end
end
def self.search(input, ids)
return nil if input == ""
User.where(['name LIKE ?', "%#{input}%"] ).where.not(id: ids).limit(10)
end
Recommended Posts