0

I have two forms. One of which has two fields, the name field, and a field called original_number. When the form POSTs, it leads to another form, and one thing I'd like to do is to store the name field, and the original_number field, as hidden values in the second form.

However, I'm not sure how to make it so that for the value field of the second form, rails receives the information from the POST method.

How would I make it so that for the value of the hidden field, the information that from the first form's POST is used?

Here's my first form

  <%= form_tag("/find_numbers", :method => "post", :id => "new_user" ) do %>
    <%= render 'shared/error_messages' %>    

    <%= label_tag(:name, "What Are You Tracking?") %>
     <%= text_field_tag(:name) %>

    <%= label_tag(:original_number, "Your Original Number") %>
    <%= text_field_tag(:original_number) %>
    <hr class="faded" />
    <h4>Choose One Of The Methods Below To Find Your Tracking Number</h4>
    <hr class="faded" />
    <%= label_tag(:in_postal_code, "Enter Your Postal Code (e.g. 94117):") %>
    <%= text_field_tag(:in_postal_code) %>

    <%= label_tag(:near_number, "Near this other number (e.g. 4156562345)") %>
    <%= text_field_tag(:near_number) %>

    <%= label_tag(:contains, "Matching this pattern (e.g. 415***EPIC):") %>
    <%= text_field_tag(:contains) %>  

    <%= submit_tag("Search", :class => "btn btn-large btn-primary") %>


  <% end %>

And here's my second form, which receives information from the first form :

<%= @numbers.each do |number| %>

    <%= form_tag(:controller => "phones", :action => "create" ) do %>
        <%= hidden_field(:original_number) %>
        <%= hidden_field(:name) %>
        <%= hidden_field(:twilio_number, :value => number.phone_number) %>

        <div class="found_list">
            <div class="found_phone_number">
                <%= label_tag(:number, number.friendly_name) %>
            </div>
            <div class="choose_found_number">
             <%= submit_tag("Choose This Number", :class => "btn btn-large btn-success") %>
            </div>
        </div>
            <hr>
    <% end %>
<% end %>

Note the

        <%= hidden_field(:original_number) %>
        <%= hidden_field(:name) %>

Note the request parameters the first form sends,

    Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"RMvjP9PQt9hVEJE449pMqgqnFcreqvXtmZFEyU+641g=",
 "name"=>"Stepan Parunashvili",
 "original_number"=>"1231231234",
 "in_postal_code"=>"",
 "near_number"=>"",
 "contains"=>"415"}

how would I get the "name"=>"Stepan Parunashvili", to fill up the hidden field for :name, and the "original_number"=>"1231231234", to fill up the field for the hidden field :original_number?

Thank you for bearing with me until now, any help would be greatly appreciated!

2 Answers 2

2

try this

          <%= hidden_field :original_number, params[:original_number] %>
          <%= hidden_field :name, params[:name] %>
2
  • Nice! Aayush, it worked. One typo in there though, instead of param, it was params. Thanks for such a quick response! Oct 4, 2012 at 11:47
  • accept the answer so that no other person try for any other non existing solution Oct 4, 2012 at 11:49
2

There are two versions of hidden_field method: One is an instance method of the ActionView::Helpers::FormBuilder class, which takes one parameter, and one is a helper method in ActionView::Helpers::FormHelper, which has two mandatory arguments. Since you're not using a form helper, you're calling this second version, which gives you the argument error.

Simple fix: Use the hidden_field_tag method instead:

hidden_field_tag(:original_number)

or if you also want to provide the value:

hidden_field(:original_number, params[:original_number)

Aayush's answer works as well, but this way your form is constisten, since you use the *_tag methods for your other form fields.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.