autocomplete field is empty when editing form

烂漫一生 提交于 2019-12-12 02:53:52

问题


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

    <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>

回答1:


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 : '' %>
...

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



来源:https://stackoverflow.com/questions/35793814/autocomplete-field-is-empty-when-editing-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!