在我的 Rails 5 應用程式中,我想已經提交 text_field_tag by current_login.user.full_name。此外,我想將 current_login.user.idtext_field作為引數 [:physician_id] 在其中傳遞。我所做的很簡單:
<%= text_field_tag "physician_id", current_login.user.id, class: 'form-control', value: current_login.user.full_name, disabled: true %>
有了這段代碼,我得到了:
pry> params['physician_id']
=> nil
如果我添加<%= text_field_tag "physician_id", id: current_login.user.id (...)我有:
pry> params['physician_id']
=> {:id=>70, :class=>\"form-control\", :value=>\"Gary KZM JohnsonR\", :disabled=>true}
如何將其current_login.user.id作為params['physician_id']內部 text_field_tag 傳遞?我應該用別的東西嗎?
uj5u.com熱心網友回復:
作為參考,方法簽名是
text_field_tag(name, value = nil, options = {})
您不能同時擁有current_login.user.id和value:指定,它們都映射value到input. 您的輸入也被禁用,因此它不會與表單一起提交。
<%= text_field_tag "physician_id", current_login.user.id, class: "form-control",
value: current_login.user.full_name, disabled: true %>
您正在尋找一個 select_field_tag 或者有單獨physician_name的輸入和一個physician_id作為隱藏輸入
<%= text_field_tag "physician_name", current_login.user.full_name, class: "form-control" %>
<%= hidden_field_tag "physician_id", current_login.user.id, class: "form-control" %>
這將提交為params
{"physician_id"=>"1", "physician_name"=>"name"}
但是我建議您不要這樣做,因為這不是處理 current_user 屬性的安全方法。我可以提交任何 ID 作為醫生 ID,并且可能訪問不屬于我的記錄。您應該在控制器內部分配這些屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/452374.html
標籤:轨道上的红宝石
