6

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 class="field">
    <%= f.label :weight %><br>
    <%= f.text_field :weight %>
  </div>
  <div class="field">
    <%= f.label :heartrate %><br>
    <%= f.text_field :heartrate %>
  </div>
  <div class="field">
    <%= f.label :bodyfat %><br>
    <%= f.text_field :bodyfat %>
  </div>

    <%= hidden_field_tag :client_id, @client.id %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
1
  • where you are setting @client object? and how? Commented Oct 4, 2017 at 13:00

5 Answers 5

15

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" id="request_client_id" name="request[client_id]" value="1" />

and when you used

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

it change to html

   <input id="client_id" 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.

Sign up to request clarification or add additional context in comments.

Comments

8

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.

Comments

3

You should use:

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

Some source:

Comments

3

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.