Forum: Ruby on Rails Save item to DB x times (howto)?

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.
Eed3ca2591560a2dd91222d9b62f882d?d=identicon&s=25 Ryan Prins (Guest)
on 2006-04-18 05:15
(Received via mailing list)
Hi All,

I have a pretty simple question. I have a view that offers the user the
following:

   1. A text field to enter in a passphrase
   2. Another text field to define how many times that phrase is written
   to the DB

It probably doesn't make sense for it to be written to the DB x times,
but
this is the first step in a process and the passphrase will become
unique.

I currently don't have a problem saving one phrase to the database at a
time, but I am a little unclear on how, at the controller level, to have
it
post to the database x times. I am stumped on how I'd have the text
field
(#2 above) look in the view and how I'd access it and then complete a
for
loop over it and save a new row to the table each time until the count
is
reached.

Any ideas on how I'd be able to do this? Thanks in advance!
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-04-18 05:18
(Received via mailing list)
On 4/17/06, Ryan Prins <rprins@gmail.com> wrote:
> I currently don't have a problem saving one phrase to the database at a
> time, but I am a little unclear on how, at the controller level, to have it
> post to the database x times. I am stumped on how I'd have the text field
> (#2 above) look in the view and how I'd access it and then complete a for
> loop over it and save a new row to the table each time until the count is
> reached.
>
> Any ideas on how I'd be able to do this? Thanks in advance!
>

I have no idea why you'd want to do this, but I'll assume you can
handle yourself. Heh.

Assuming your view has something like:
<%= text_field 'passphrase', 'save_count' %>

In the controller action that posts to, you can say:
count = params[:passphrase][:save_count].to_i rescue 0
count.times do
  Passphrase.create params[:passphrase]
end
This topic is locked and can not be replied to.