In my Rails 5 app I want to have already filed text_field_tag by current_login.user.full_name
. Additionally I want to pass current_login.user.id inside this text_field
as params[:physician_id]. What I did is pretty simple:
<%= text_field_tag "physician_id", current_login.user.id, class: 'form-control', value: current_login.user.full_name, disabled: true %>
With this code I've got:
pry> params['physician_id']
=> nil
If I add <%= text_field_tag "physician_id", id: current_login.user.id (...)
I've got:
pry> params['physician_id']
=> {:id=>70, :class=>\"form-control\", :value=>\"Gary KZM JohnsonR\", :disabled=>true}
How to pass this current_login.user.id
as a params['physician_id']
inside text_field_tag? should I use something else?
CodePudding user response:
For reference, the method signature is
text_field_tag(name, value = nil, options = {})
You cannot have both current_login.user.id
and value:
specified, they both map to value
attribute of the input
. Also your input is disabled so it is not being submitted with the form.
<%= text_field_tag "physician_id", current_login.user.id, class: "form-control",
value: current_login.user.full_name, disabled: true %>
Either you're looking for a select_field_tag or have separate physician_name
input and a physician_id
as hidden input
<%= 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" %>
This will submit as params
{"physician_id"=>"1", "physician_name"=>"name"}
However I suggest you don't do that, as this is not a secure way to deal with current_user attributes. I could submit any id as physician_id and probably get access to records that don't belong to me. You should assign these attributes inside of your controller instead.