编辑表单时,“自动完成"字段为空 [英] autocomplete field is empty when editing form

查看:66
本文介绍了编辑表单时,“自动完成"字段为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a sample model in which belongs_to a patient model.Using rails-jquery-autocomplete I have managed to implement an autocomplete field where one can search patient's code and it works well. However when editing the form, the patient code is empty on the form. How should I fix it?

App/views/sample/_form.html.erb

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>

推荐答案

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

I faced the same issue while editing the saved model, a quick workaround would be as follows:

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

假定实例变量为@sample.根据您的方案进行修改.干杯!

Assuming that the instance variable is @sample. Modify it according to your scenario. Cheers!

这篇关于编辑表单时,“自动完成"字段为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆