【问题标题】:autocomplete field is empty when editing form编辑表单时自动完成字段为空
【发布时间】:2016-03-04 10:35:06
【问题描述】:

我有一个示例模型,其中属于患者模型。使用rails-jquery-autocomplete 我设法实现了一个自动完成字段,可以在其中搜索患者的代码并且效果很好。但是,在编辑表格时,表格上的患者代码为空。 我该如何解决?

App/views/sample/_form.html.erb

    <div class="field">
       <%= f.label :patient_code %><br>
       <%= f.hidden_field :patient_id, id: 'patient_id' %>
       <%= f.autocomplete_field :patient_code, autocomplete_patient_code_samples_path, id_element: '#patient_id' %>
    </div>

【问题讨论】:

  • 您的示例中没有任何内容看起来有问题,而且我认为您发布的内容还不够多,让我们看看有什么问题。我建议检查文档,因为问题可能出在代码的其他部分:github.com/bigtunacan/rails-jquery-autocomplete 我最初的怀疑是id_element 有问题。当您打开浏览器检查器控制台时,您是否有 JS 错误?
  • 不,我没有收到任何错误。 [这里是我的实现的更多细节的链接] (stackoverflow.com/questions/35573168/…)

标签: ruby-on-rails-4 jquery-autocomplete


【解决方案1】:

我在编辑保存的模型时遇到了同样的问题,一个快速的解决方法如下:

...
<%= f.autocomplete_field :patient_code, autocomplete_patient_code_samples_path, id_element: '#patient_id', value: (!@sample.new_record?)? @sample.patient.code : '' %>
...

假设实例变量是@sample。根据你的场景修改它。干杯!

【讨论】:

  • @user1876128 很抱歉我的三元运算符语法错误。请检查我的更新答案。
  • 这可以稍作改动,您可以更新您的答案以便我批准,&lt;%= f.autocomplete_field :patient_code, autocomplete_patient_code_samples_path, id_element: '#patient_id', value: (!@sample.new_record?)? @sample.patient.code : '' %&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
相关资源
最近更新 更多