Forum: Ruby on Rails Link_To_Remote Incrementing

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.
Pavel T. (Guest)
on 2007-06-19 03:05
Hey folks,

I'm new to Ruby and Rails, so I'd like to ask you a quick question on
how to make my program work as I need to.

What I want to do is, have a link_to_remote called "Click Me!" which,
upon clicking, will create an input field like the following:

<input type="file" name="image0" />

That in itself is a piece of cake, but I'd also like to increment the
number. For example, if the user wants to upload 300 images, they can
click 300 times and the end result will be like:

<input type="file" name="image0" />
<input type="file" name="image1" />
<input type="file" name="image2" />
<input type="file" name="image3" />
...
<input type="file" name="image299" />

Any ideas as to go about doing this? In the mean time, I'm using actual
JavaScript...
Eric A. (Guest)
on 2007-06-19 03:26
(Received via mailing list)
Paul C. wrote:
> Any ideas as to go about doing this? In the mean time, I'm using actual
> JavaScript...

I would just stick to using actual JavaScript. No point in making the
user wait for a request/response cycle.

Eric
Pavel Tzun (Guest)
on 2007-06-20 00:02
Eric A. wrote:
> Paul C. wrote:
>> Any ideas as to go about doing this? In the mean time, I'm using actual
>> JavaScript...
>
> I would just stick to using actual JavaScript. No point in making the
> user wait for a request/response cycle.
>
> Eric

The problem here is if some guy clicked 200 times and after selecting
the files he wanted, he decided he needed 201 files, then another click
would reset the first 200 fields, leaving him with 201 empty fields.

If there is no way of implementing this with Rails, then I can just cry
to myself at night time. I just hope there is.
Bob S. (Guest)
on 2007-06-20 00:33
(Received via mailing list)
On 6/18/07, Paul C. <removed_email_address@domain.invalid> wrote:
>
> That in itself is a piece of cake, but I'd also like to increment the
> number. For example, if the user wants to upload 300 images, they can
> click 300 times and the end result will be like:
>
> <input type="file" name="image0" />
> <input type="file" name="image1" />
> <input type="file" name="image2" />
> <input type="file" name="image3" />
> ...
> <input type="file" name="image299" />

If you name all your inputs "image[]", you'll get an array back in
params.

You might want to look at this as well:
http://rubyonrailsnotes.blogspot.com/2007/03/multi...
Pavel T. (Guest)
on 2007-06-20 02:55
> If you name all your inputs "image[]", you'll get an array back in
> params.
>
> You might want to look at this as well:
> 
http://rubyonrailsnotes.blogspot.com/2007/03/multi...

The array isn't the problem at hand. My problem is making the <input
type="file"> have names that increment when you click the "add image"

so after I click once, the page will contain:

<input type="file" name="image[0]" />

Two more clicks put this on the screen:

<input type="file" name="image[0]" />
<input type="file" name="image[1]" />
<input type="file" name="image[2]" />

If you can help with this, I would love you forever.
Eric A. (Guest)
on 2007-06-20 03:28
(Received via mailing list)
Pavel Tzun wrote:
> The array isn't the problem at hand. My problem is making the <input
> type="file"> have names that increment when you click the "add image"

I'm not sure why you need to increment the number. Just name it
"image[]" and it will be an array instead of a hash when the params
receive it. Something like this should work:

<div id="upload">
   <%=file_field_tag 'image[]'%>
   <%=link_to_function 'Add Another', "new Insertion.Bottom('upload',
     escape_javascript(file_field_tag 'image[]'))"%>
</div>

The above is not tested but something like that should work.

Eric
Pavel T. (Guest)
on 2007-06-20 03:53
Eric A., thank you so much! I finally got this hunk-o-junk
working.

I'm new to the RoR scene, so this whole Array thing makes my life 100.2
times easier.

Like I promised, I love you forever.
This topic is locked and can not be replied to.