0

Ruby on Rails 4.1

The form is placing the parameters into an array when POST is done. I am making a script to auto fill the billing address the same as the shipping address. When the script runs it is assigning the values as html values.

How do you put these values into the payment array being sent?

The log where cc_name and ship_name are outside the payment array, I want them inside:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"x", "cc_name"=>"Bobby", "payment"=>{"telephone"=>"555", "email"=>"[email protected]", "address1"=>"641 W Rt 66", "address2"=>"", "city"=>"Glendora", "state"=>"CA", "postal_code"=>"91740", "country"=>"USA", "cc_type"=>"Visa", "cc_number"=>"5555555588884444", "ccv"=>"321", "expires_on(1i)"=>"2014", "expires_on(2i)"=>"9", "expires_on(3i)"=>"1", "ship_address1"=>"641 W Route 66", "ship_address2"=>"", "ship_city"=>"Glendora", "ship_state"=>"c", "ship_postal_code"=>"91740", "ship_country"=>"u", "card_holder_auth"=>"1", "charge_auth"=>"1", "name_auth"=>"David Duke", "date_auth"=>"7"}, "billingtoo"=>"on", "ship_name"=>"Bobby", "commit"=>"Send Payment"}

The form (minus clutter):

<%= form_for @payment do |f| %>

<%= render 'shared/payment_error_messages' %>

<h1>Fill in all blank areas with payment information:</h1>
<div class="col-md-5 col-md-offset-1">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Applicant Information</h3>
        </div>
        <div class="panel-body">
        <%= f.label :cc_name, "Name as it appears on credit card:" %><br>
        <%= f.text_field :cc_name, class: "input-lg", :name => "cc_name" %> <%#= @payment.errors.get(:cc_name) %>

        <%= f.label :telephone, "Telephone Number:" %><br>
        <%= f.text_field :telephone, class: "input-lg" %>
        ...
        <div class="panel-body">
        <input type="checkbox" name="billingtoo" onclick="FillBilling(this.form)"><em>Shipping Address is the same as the Billing Address</em>
        <p>Please Note: This cannot be a P.O box address</p>

        <%= f.label :ship_name, "Name:" %><br>
        <%= f.text_field :ship_name, class: "input-lg", :name => "ship_name" %>

        <%= f.label :ship_address1, "Address 1:" %><br>
        <%= f.text_field :ship_address1, class: "input-lg" %>
        ...

        <%= f.submit "Send Payment", class: "btn btn-lg btn-primary", id: "commit" %>
        </div>
    </div>
</div>
<% end %>
<br/>
<%= link_to 'Back', session[:last_page] %>
</div>

<script>
function FillBilling(f) {
  if(f.billingtoo.checked == true) {
  f.ship_name.value = f.cc_name.value;

  }
}
</script>

1 Answer 1

1

You are overwriting the field's name:

:name => "cc_name"

That's the reason why it is sent outside of the array. Try removing the :name assigning:

<%= f.text_field :cc_name, class: "input-lg" %>

and

<%= f.text_field :ship_name, class: "input-lg" %>
Sign up to request clarification or add additional context in comments.

5 Comments

I am using the :name assignment in the javascript that is writing to the other fields when the checkbox is checked.
The name by default will be payment[cc_name] and payment[ship_name] respectively. From the javascript you can refer to them in that way, or even better, giving an id to the field.
I tried using those names but the script would complain the value is undefined. If I type one in it says the other is undefined. Uncaught ReferenceError: ship_name is not defined
you need to refer to those fields as: f.['payment[cc_name]'].value and f.['payment[ship_name]'].value respectively.
Oh I see! +1 for the details. Also, using the id worked well. :D

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.