문제

I'm trying to integrate jQuery UI Autocomplete in my Rails 4.0 application like described in this Railscast: 102-AutoCompleteAssociationRevised.

Unfortunately, the autocompletion doesn't work. I don't get any suggestions although the rendered HTML looks fine:

<input id="plan_part_name" type="text" name="plan[part_name]" data-autocomplete-source="["foo","food","foobar"]"></input>

I also deactivated Turbolinks, but nothing changed...

In my Gemfile I included gem 'jquery-ui-rails' and my application.js looks like this:

//= require jquery
//= require jquery.ui.all
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require jquery-fileupload/basic
//= require jquery-fileupload/vendor/tmpl
//= require_tree .

This is the form, that I'm using, edit.html.erb:

<%= form_for [@project, @plan], remote: true, html: { id: "edit-form-#{@plan.id}" } do |f| %>
  <%= f.text_field :title %>

  <%= f.text_field :part_name, data: {autocomplete_source: %w[ foo food foobar ]}  %>

  <%= f.submit "Update" %>
<% end %>

autocomplete.js.coffee:

autocomplete = ->
  $('#plan_part_name').autocomplete
    source: $('#plan_part_name').data('autocomplete-source')

$(document).ready(autocomplete)
$(document).on('page:load', autocomplete)
도움이 되었습니까?

해결책

I found the solution:

My edit form got rendered by Ajax request so I had to change the Javascript file (autocomplete.js.coffee) from:

$(document).on('page:load', autocomplete)

to:

$(document).on('page:update', autocomplete)

to work work properly with Turbolinks. You'll find more information in the Turbolinks Readme.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top