Forum: Ruby on Rails Multiple Values in Form Field and Creating Multiple Records

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David L. (Guest)
on 2006-12-29 20:31
Hi,

I want to be able to have users enter one or more email addresses in a
field and have a database row created for each of those email addresses.

The contents of the database row would be:

created_on
job_id
user_id
email_address

Here is an example of my form output:

{"commit"=>"Send", "email"=>{"created_on"=>"12-29-2006 @ 14:12 PM",
"job_id"=>"1", "user_id"=>"4", 
"email_address"=>"removed_email_address@domain.invalid,
removed_email_address@domain.invalid, removed_email_address@domain.invalid"}}

I can easily get the values for created_on, job_id, and user_id, but how
do I iterate over the email addresses?

Thanks,

David
Bill W. (Guest)
on 2006-12-29 21:31
(Received via mailing list)
Hi David,

David L. wrote:

> I want to be able to have users enter one or more email addresses in a
> field and have a database row created for each of those email addresses.
>
> Here is an example of my form output:
>
> "email_address"=>"removed_email_address@domain.invalid,
> removed_email_address@domain.invalid, removed_email_address@domain.invalid"}}
>
> how do I iterate over the email addresses?
Assuming your email address table was named something like 'addresses'
you
could do something like...

params[:email_address].each(',') {|this_address|
   address_to_add = Address.new
   address_to_add.created_on = params[:created_on]
   address_to_add.job_id = params[:job_id]
   address_to_add.user_id = params[:user_id]
   address_to_add.email_address = this_address
   address_to_add.save
}

hth,
Bill
This topic is locked and can not be replied to.