I’ve got a form with some upload boxes, and I’d really like to add more
boxes when the user clicks on a link, a la Gmail attachments. I was
thinking I could just use any javascript for this, it’s not hard, but I
had two thoughts:
- Rails is gravitating towards less dependency on typing your own
javascript, for example RJS - I’m not just inserting regular textfields, I’m inserting
text_field_with_auto_complete, which gets pretty messy if I want to put
that in a JavaScript function somehow. I want to make this as simple as
possible.
So here’s what I’ve got at the moment.
<%= link_to_function ‘add another song’,
update_page { |page| page.insert_html :before, ‘album_submit’,
“Name:” + text_field_with_auto_complete(:songs, :name,
:index => 2) +
" " + file_field_tag(‘songs[2][uploaded_data]’) + ‘
’
} %>
The problem is, I’ve got the number “2” hardcoded in there. I don’t want
to reference “2” here in the index, I want to reference something that
will be incremented every time this is called. Now, if I have it
reference a Ruby value, that doesn’t help me, because this block is
evaluated when the page is loaded, not when the link is clicked.
Ideas:
- Reference a JavaScript function somehow. This is kind of naive, as
both the ID and NAME of the text field will have this index value. - Instead of making this inline, make this true AJAX, and a ruby
variable can be incremented on the remote side. This is overkill,
though, there’s no reason why I should be executing anything on the
server side just to add fields to my form.
I’ve been fighting with this something fierce, so any advice would be
much appreciated.