How to use hidden field tag in rails

Say I have this form, but already have a variable, client_id, that I want to pass to the controller. It looks like I need to use a hidden_field_tag. I tried it but I don't think my syntax is quite right. Any idea what i'm doing wrong? Thanks

<%= form_for(@assessment) do |f| %> <div> <%= f.label :weight %><br> <%= f.text_field :weight %> </div> <div> <%= f.label :heartrate %><br> <%= f.text_field :heartrate %> </div> <div> <%= f.label :bodyfat %><br> <%= f.text_field :bodyfat %> </div> <%= hidden_field_tag :client_id, @client.id %> <div> <%= f.submit %> </div>
<% end %>
1

5 Answers

Please refer following links hidden_field or hidden_field_tag

 <%= f.hidden_field :client_id, 1 %> <%= hidden_field_tag 'client_id', '1' %>

Note when you used

 <%= f.hidden_field :client_id, 1 %>

it change to html

<input type="hidden" name="request[client_id]" value="1" />

and when you used

<%= hidden_field_tag 'client_id', '1' %>

it change to html

 <input name="client_id" type="hidden" value="1"/>

So, I think here you should use <%= f.hidden_field :client_id, @client.id %>

Hope it will work for you.

Try doing:

<%= f.hidden_field :client_id, value: @assessment.client_id %>

Since, as in your prior question, you are setting @assessment.client_id in your new action.

0

You should use:

<%= f.hidden_field :client_id, value: @client.id %>

Some source:

<%= f.hidden_field :client_id, :value => @assessment.client_id %>

Should the client_id be part of the assessment_params? If so, you can attach it to the form object, i.e. <%= f.hidden_field :client_id, value: @client.id.

Otherwise I think your tag should pass through, just outside of the assessment params hash.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like