简体   繁体   English

jQuery UI自动完成与simpleform

[英]jquery-ui autocomplete with simpleform

I just spent some time with this issue, and if I did, maybe someone else does too. 我只是花了一些时间来解决这个问题,如果我这样做了,也许其他人也做了。 So I'll just place the question and then the answer... 所以我只问一个问题,然后回答...

I'm running on Rails 5 and trying to dry my code a bit, and changing from form_for to simple_form_for. 我在Rails 5上运行,并尝试将代码干燥一些,然后从form_for更改为simple_form_for。

with form_for, I managed to get autocomplete working by following this tutorial and addressing the turbo-links issue too. 使用form_for,通过遵循本教程并解决了turbo-links问题,我设法实现了自动完成工作。

and have: under _form.html.erb ... 并在_form.html.erb下...

<div class="form-group">
  <%= f.label :cae_id, class: "col-md-5 control-label" %>
  <div class="col-md-7">
    <%= f.text_field :cae_codenr, data: { autocomplete_source: index_autocomplete_caes_path, remote: true} %>
  </div>
</div>

...

and companies.coffee 和companies.coffee

$(document).on 'turbolinks:load', ->

  jQuery ->
    $('#company_cae_codenr').autocomplete source: $('#company_cae_codenr').data('autocomplete-source') 
  jQuery ->
    $('#user_company_attributes_cae_codenr').autocomplete source: $('#user_company_attributes_cae_codenr').data('autocomplete-source')

and my caes_controller: 和我的caes_controller:

def index_autocomplete
   @caes = Cae.order(:codenr).where("codenr like ?", "%#{params[:term]}%")

   render json: @caes.map(&:codenr)
end

and my company.rb 和我的company.rb

...
  def cae_codenr
    cae.try(:codenr)
  end

  def cae_codenr=(codenr)
    self.cae = Cae.find_by(codenr: codenr) if codenr.present?
  end
...

when I change 当我改变

  <div class="form-group">
    <%= f.label :cae_id, class: "col-md-5 control-label" %>
    <div class="col-md-7">
      <%= f.text_field :cae_codenr, data: { autocomplete_source: index_autocomplete_caes_path, remote: true} %>
    </div>
  </div>

with matching html code: 与匹配的html代码:

  <div class="form-group">
    <label class="integer required col-md-5 control-label" for="company_cae_id">
       <abbr title="required">*</abbr> 
       Cae
    </label>
    <div class="col-md-7">
       <input data-autocomplete-source="/caes/index_autocomplete" data-remote="true" type="text" name="company[cae_codenr]" id="company_cae_codenr" />
    </div>
  </div>

to

<%= f.input :cae_codenr, data: { autocomplete_source: index_autocomplete_caes_path, remote: true} %>

with the following HTML 使用以下HTML

   <div class="input string optional company_cae_codenr">
    <label class="string optional" for="company_cae_codenr">Cae codenr</label>
     <input class="string optional" type="text" name="company[cae_codenr]" id="company_cae_codenr" />
   </div>

and when I start entering a number in my input I get 当我开始在输入中输入数字时,我得到

this.source is not a function this.source不是函数

error 错误

So...I've looked for solutions around jquery and simpleform, and also this error message, but not any of the direct solutions seem to work here... 所以...我一直在寻找有关jquery和simpleform的解决方案,以及此错误消息,但似乎没有任何直接的解决方案在这里起作用...

The solution for this actually came to me while I was writing the question, and decided to paste the html source. 实际上,在我编写问题时就找到了解决方案,并决定粘贴html源代码。

So it's quite a lesson here as a way to debug when one knows something that's working, and something that is supposed to do similar stuff not working. 因此,这是一个相当不错的课程,可作为一种调试方法,以便在人们知道某事在起作用,而某事应该做类似的事情却无法起作用时进行调试。

As soon as I compared both html code, I noticed the data-autocomplete attribute was not being sent with simple form. 当我比较两个html代码时,我注意到data-autocomplete属性没有以简单的形式发送。 And then the question just came: How to pass data attributes to simple form? 然后问题来了:如何将数据属性传递给简单形式?

and voilá, stackoverflow has the answer: https://stackoverflow.com/a/8332537/1461972 , and so in the end I ended up with: 和voilá,stackoverflow有答案: https ://stackoverflow.com/a/8332537/1461972,所以最终我得到了:

  <%= f.input :cae_codenr, input_html: {data: { autocomplete_source: index_autocomplete_caes_path}} %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM