# Creating hash and putting form content into it

**URL:** <https://www.ruby-forum.com/t/creating-hash-and-putting-form-content-into-it/129147>\
**Category:** Rails\
**Created:** [February 14, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/creating-hash-and-putting-form-content-into-it/129147 "2008-02-14T21:09:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [February 14, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/creating-hash-and-putting-form-content-into-it/129147/1 "2008-02-14T21:09:16Z")

</div>

I’ve got a form with fields of this naming convention:  
  
First Name: \<input type=“text” name=“customer[first\_name]” …  
Last Name: \<input type=“text” name=“customer[last\_name]” …  
Address: \<input type=“text” name=“customer[address\_1]” …  
City: \<input type=“text” name=“customer[city]” …  
State: \<input type=“text” name=“customer[state]” …  
  
First Name: \<input type=“text” name=“billing\_address[first\_name]”…  
Last Name: \<input type=“text” name=“billing\_address[last\_name]”…  
Address: \<input type=“text” name=“billing\_address[address\_1]”…  
City: \<input type=“text” name=“billing\_address[city]”…  
State: \<input type=“text” name=“billing\_address[state]”…

And I can successfully use params[:customer] to access all the  
customer items and params[:billing\_address] for the billing info. And  
I can successfully use Hash(params[:customer]) to create a new  
customer and populate a table. The problem I’m having is if they  
identify that the billing info is the same as their customer info,  
then I want to put the customer info into a billing hash to use to  
update/create a record in the billing table. This code doesn’t work  
for creating the hash (but doesn’t generate an error either):

```
begin
  billing_address_hash = Hash.new()
  if (params[:different_billing_info] == "1")
    billing_address_hash.merge(params[:billing_address])
    #puts("\nDifferent: ", billing_address_hash.to_s)
  else
    billing_address_hash.merge(params[:customer])
    #puts("\nSame: ", billing_address_hash.to_s)
    # Remove customer fields not found in the billing address

```

table  
billing\_address\_hash.delete(‘status’)  
billing\_address\_hash.delete(‘email’)  
end  
rescue  
@error\_message = “Unable to create billing address hash.  
”  
@request\_completed = false  
return  
end

It just returns the initialized new billing\_address\_hash as empty. If  
I use Hash(params[:billing\_address]) (or customer) in place for the  
parameter for merge, an error occurs and the error message is  
returned.

How do I get params[] data into a hash that the Hash merge method will  
take as a parameter?

The reason I’m doing something like this is that I have additional  
fields in the customer model that are not in the billing\_address  
model. I’m assuming if I try to pass all the customer fields into the  
billing model, it will cause an error.

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [February 14, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/creating-hash-and-putting-form-content-into-it/129147/2 "2008-02-14T21:25:26Z")

</div>

Jack wrote:  
:

> ```
> begin
> billing_address_hash = Hash.new()
> if (params[:different_billing_info] == "1")
> billing_address_hash.merge(params[:billing_address])
> #puts("\nDifferent: ", billing_address_hash.to_s)
> else
> billing_address_hash.merge(params[:customer])
> #puts("\nSame: ", billing_address_hash.to_s)
> # Remove customer fields not found in the billing address
> 
> ```
> 
> table  
> billing\_address\_hash.delete(‘status’)  
> billing\_address\_hash.delete(‘email’)  
> end  
> rescue  
> @error\_message = “Unable to create billing address hash.  
> ”  
> @request\_completed = false  
> return  
> end
> 
> It just returns the initialized new billing\_address\_hash as empty. If

Can you try with merge! instead of merge ?

Stephan

# irb

> > a = {:a =\> 1}; b = {:b =\> 2}  
> > =\> {:b=\>2}
> 
> > a.merge(b)  
> > =\> {:b=\>2, :a=\>1}
> 
> > a  
> > =\> {:a=\>1}
> 
> > b  
> > =\> {:b=\>2}
> 
> > a.merge!(b)  
> > =\> {:b=\>2, :a=\>1}
> 
> > a  
> > =\> {:b=\>2, :a=\>1}
> 
> > b  
> > =\> {:b=\>2}
> 
> > ‘good luck’

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [February 14, 2008, 10:06pm UTC](https://www.ruby-forum.com/t/creating-hash-and-putting-form-content-into-it/129147/3 "2008-02-14T22:06:26Z")

</div>

Thanks Stephan. You at least pointed me to the correct method. merge  
returns a new hash, which I wasn’t saving. But merge! adds content to  
the existing hash. I’m still getting an empty hash though using  
something like this:

```
billing_address_hash = Hash.new()
begin
  if (params[:different_billing_info] == "1")
    billing_address_hash.merge!(Hash[params[:billing_address]])
    logger.info("\nDifferent: ", billing_address_hash.to_s)
  else
    billing_address_hash.merge!(Hash[params[:customer]])
    logger.info("\nSame: ", billing_address_hash.to_s)
    # Remove customer fields not found in the billing address

```

table  
billing\_address\_hash.delete(‘uva\_status’)  
billing\_address\_hash.delete(‘email’)  
end  
rescue  
@error\_message = “Unable to create billing address hash.  
”  
@request\_completed = false  
return  
end

which baffles me because I use Hash[params[:customer]] to pass the  
form data to the customer model when creating a new one and it works  
just fine.

On Feb 14, 3:25 pm, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [February 14, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/creating-hash-and-putting-form-content-into-it/129147/4 "2008-02-14T23:06:00Z")

</div>

Jack wrote:

If you post more controller code, that should make it easier to track  
down.

Stephan

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [February 15, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/creating-hash-and-putting-form-content-into-it/129147/5 "2008-02-15T14:22:20Z")

</div>

Here’s the entire method that is when the form is submitted.  
Everything up to the billing\_address\_hash works. (Note: Before adding  
that section the last if construct, ie. @request.agency\_id was  
functioning successfully when it was passed  
Hash[params[:billing\_address]] as the last parameter. I needed to  
change this in the event an external customer did not include a  
separate billing address.)

# Save all the details entered for a request to the database, making

sure to flag the order as unapproved  
def save\_details  
# Set a flag to determine if the request data is all good if this  
method returns at the end  
# without this value being modified.  
@request\_completed = true  
@error\_message = ‘’

```
# Get the form customer contact information.
form_customer = Customer.new(Hash[params[:customer]])
# Get/create the department's id to store in the customer record
form_department_name = params[:department_name]
form_customer.department_id =

```

Department.get\_id(form\_department\_name)  
# Identify how the customer heard about the service.  
form\_heard\_about\_service\_other =  
params[:heard\_about\_service\_other]  
form\_customer.heard\_about\_service\_id =  
HeardAboutService.existing\_or\_other\_id(form\_customer.heard\_about\_service\_id,form\_heard\_about\_service\_other)  
# Determine if this is a returning customer using the email  
address. If so then  
# update their existing information. Otherwise create a new  
customer record.  
existing\_customer = Customer.find(:first, :conditions =\> [“email  
= ?”, form\_customer.email])  
begin  
if (existing\_customer != nil)  
existing\_customer.update\_attributes!(Hash[params[:customer]])  
existing\_customer.update\_attributes!({‘department\_id’ =\>  
form\_customer.department\_id, ‘heard\_about\_service\_id’ =\>  
form\_customer.heard\_about\_service\_id})  
@customer = existing\_customer  
else  
form\_customer.save!  
@customer = form\_customer  
end  
rescue  
# If any error occurs when saving the customer information then  
the request submission fails  
# since we need to be able to tie the request to a valid  
customer record.  
@error\_message += “Customer information could not be saved!  
”  
@request\_completed = false  
return  
end

```
# Get the form's general order request info.
@request = Request.new(Hash[params[:order]])
@request.is_approved = 0
# Assign the customer's id to this new order
@request.customer_id = @customer.id
# Make sure the date due is formatted correctly before writing to

```

the table  
# parsedate returns an array with year, month, day as first three  
elements.  
begin  
date\_due = ParseDate.parsedate(params[:order][:date\_due],true)  
@request.date\_due =  
Date.new(date\_due[0],date\_due[1],date\_due[2]).to\_s + ’ 17:00:00’  
rescue # if an invalid date was entered set to nil  
@request.date\_due = nil  
end  
# Set the request date for the order to the current date and time  
@request.date\_request\_submitted = Time.now.strftime(“%Y-%m-%d %H:  
%M:%S”)  
# Make sure that external customers have the availability set to  
Public  
if (@session[:computing\_id] == ‘External’)  
@request.availability = ‘Public’  
end  
# Create a new request/order record.  
begin  
@request.save!  
rescue  
# If the request cannot be saved to the order table then we  
should not continue  
@error\_message += “Unable to create a new request at this  
time.  
”  
@request\_completed = false  
return  
end

```
# NOTE: Before attempting to use the billing fields, determine if

```

the billing info is different  
# than the regular customer info. Because if the person  
affiliation is external then the billing  
# address will be empty by default and the different billing  
address box not checked.  
billing\_address\_hash = Hash.new()  
begin  
if (params[:different\_billing\_info] == “1”)  
billing\_address\_hash.merge!(Hash[params[:billing\_address]])  
logger.info("\nDifferent: “, billing\_address\_hash.to\_s)  
else  
billing\_address\_hash.merge!(Hash[params[:customer]])  
logger.info(”\nSame: ", billing\_address\_hash.to\_s)  
# Remove customer fields not found in the billing address  
table  
billing\_address\_hash.delete(‘status’)  
billing\_address\_hash.delete(‘email’)  
end  
rescue  
@error\_message += “Unable to create billing address hash.  
”  
@request\_completed = false  
return  
end

```
 # If an agency was specified on the form then search/save the

```

billing info as agency related.  
# Otherwise it should be related to the customer.  
if (@request.agency\_id != nil)  
@billing\_address =  
BillingAddress.update\_or\_create(‘agency\_id’,@request.agency\_id,billing\_address\_hash)  
logger.info(“\nBilling address update using agency”)  
else  
@billing\_address =  
BillingAddress.update\_or\_create(‘customer\_id’,@request.customer\_id,billing\_address\_hash)  
logger.info(“\nBilling address update using customer”)  
end  
end

On Feb 14, 5:06 pm, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
