2016-03-04 77 views
0

我有一個示例模型,其中belongs_to患者模型。使用rails-jquery-autocomplete我設法實現了一個自動填充字段,可以在其中搜索患者的代碼並且效果很好。但是,編輯表格時,表格上的患者代碼爲空。 我應該如何解決它?當編輯表格時,自動填充字段爲空

應用程序/視圖/樣品/ _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

您的示例中沒有任何內容出現錯誤,您沒有發佈足夠的內容,我想我們可以查看出了什麼問題。我建議檢查文檔,因爲問題可能出現在代碼的其他部分:https://github.com/bigtunacan/rails-jquery-autocomplete我最初的懷疑是'id_element'有問題。當你打開瀏覽器檢查器控制檯時,你是否有JS錯誤? – Kelseydh

+0

不,我沒有得到任何錯誤。 [這裏是我的實現的更多細節的鏈接](http://stackoverflow.com/questions/35573168/rails4-autocomplete-with-belongs-to-association) – user1876128

回答

0

我面臨着同樣的問題,同時編輯保存的模型,一個快速的解決方法是如下:

... 
<%= f.autocomplete_field :patient_code, autocomplete_patient_code_samples_path, id_element: '#patient_id', value: ([email protected]_record?)? @sample.patient.code : '' %> 
... 

假設實例變量是@sample。根據你的場景修改它。乾杯!

+0

我得到語法錯誤,意外tIVAR – user1876128

+0

@ user1876128我'對不起,我的三元運算符語法錯了。請檢查我的更新答案。 –

+0

這適用於一些小的變化,你可以更新你的答案,以便我批准''%= f.autocomplete_field:patient_code,autocomplete_patient_code_samples_path,id_element:'#patient_id',值:([email protected]_record?)? @ sample.patient.code:''%> ' – user1876128