I am trying to make @other people function in my rails application, just like stackoverflow:

I almost finished this function, but I ran into problems, the jQuery autocomputer data replaces the contents of my text area rather than add.

CoffeeScript:
find_at_sign = ->
if $("#tags").val().split('').pop() == "@"
id = $("#tags").data("article-id")
$("#tags").autocomplete
source: '/articles/' + id + '/autocomplete.json'
minLength: 1
$ ->
$(document).on("input", "#tags",
-> find_at_sign())
Article controller:
def autocomplete
@articles = Article.find_by(id: params[:article_id])
@commentor_names = @articles.comments.map(&:name).uniq
respond_to do |format|
format.html
format.json {
render json: @commentor_names
}
end
end
form_html.erb:
<div class="form-group ui-widget">
<div class="col-sm-5">
<%= f.text_area :content, rows: 5, placeholder: '说点什么...',
class: 'form-control', id: "tags", 'data-article-id': @article.id.to_s %>
</div>
</div>
I tried to use the method append, but it does not work:
$("#tags").append(${this).autocomplete
source: '/articles/' + id + '/autocomplete.json'
minLength: 1)
Any help would be appreciated!
source
share