title: Ruby on Rails - Simple Form autocomplete association search
body: <p>I've a form in a basic task management app which allows Tasks to be assigned to Users (task belongs_to user). I'm using Simple Form for this.</p>
  
  <p>Currently, the association is populated in the typical manner, with a dropdown list of users as such: <code>&lt;%= f.association :user, label_method: :name, value_method: :id, include_blank: false %&gt;</code>.</p>
  
  <p>However, as the number of Users grows, I'm looking to change this to an autocomplete form field to find users. I've tried following the <a href="http://railscasts.com/episodes/102-auto-complete-association-revised" rel="noreferrer">Railscast on the subject</a>, though it doesn't use Simple Form and isn't working on my implementation.</p>
  
  <p>Here's my form, model and .coffee code:</p>
  
  <p><strong>Form</strong></p>
  
  <pre><code>&lt;%= simple_form_for @task do |f| %&gt;
    ...
    &lt;%= f.input :user_name, as: :search, data: 
        {autocomplete_source: User.pluck(:name)} %&gt;
    ...
    &lt;%= f.button :submit %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code># Virtual attribute for autocomplete form field
    def user_name
      user.try(:name)
    end
  
    def user_name=(name)
      self.user = User.find_by_name(name) if name.present?
    end
  </code></pre>
  
  <p><strong>tasks.coffee</strong></p>
  
  <pre><code>jQuery -&gt;
    $('#task_user_name').autocomplete
      source: $('#task_user_name').data('autocomplete-source')
  </code></pre>
  
  <p>This generates the following HTML:</p>
  
  <pre><code>&lt;input class="string search optional form-control 
  ui-autocomplete-input ui-autocomplete-loading" type="search" 
  value="Robert Strong" name="task[user_name]" id="task_user_name" 
  autocomplete="off"&gt;
  </code></pre>
  
  <p>Though the following error in the dev tools console: </p>
  
  <pre><code>Uncaught TypeError: this.source is not a function
  </code></pre>
  
  <p>Concerned about the <code>autocomplete="off"</code> within the HTML and can't imagine the TypeError helps much!</p>
  
  <p>I'm stumped, so would greatly appreciate any advice on getting this working! Thanks in advance, let me know what you think, Steve.</p>