Rails 将预定义的 ID 传递给 text_field_tag
Rails pass predefined id to text_field_tag
在我的 Rails 5 应用程序中,我想 current_login.user.full_name
已经提交了 text_field_tag。另外我想在这个 text_field
中传递 current_login.user.id 作为参数 [: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
作为 text_field_tag 中的 params['physician_id']
传递?我应该使用其他东西吗?
作为参考,方法签名是
text_field_tag(name, value = nil, options = {})
您不能同时指定 current_login.user.id
和 value:
,它们都映射到 input
的 value
属性。此外,您的输入已被禁用,因此不会与表单一起提交。
<%= 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 提交为 physician_id,并且可能会访问不属于我的记录。您应该在控制器内部分配这些属性。
在我的 Rails 5 应用程序中,我想 current_login.user.full_name
已经提交了 text_field_tag。另外我想在这个 text_field
中传递 current_login.user.id 作为参数 [: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
作为 text_field_tag 中的 params['physician_id']
传递?我应该使用其他东西吗?
作为参考,方法签名是
text_field_tag(name, value = nil, options = {})
您不能同时指定 current_login.user.id
和 value:
,它们都映射到 input
的 value
属性。此外,您的输入已被禁用,因此不会与表单一起提交。
<%= 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 提交为 physician_id,并且可能会访问不属于我的记录。您应该在控制器内部分配这些属性。