# Ajax file upload question

**URL:** <https://www.ruby-forum.com/t/ajax-file-upload-question/173393>\
**Category:** Rails\
**Created:** [August 7, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/ajax-file-upload-question/173393 "2009-08-07T22:56:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [August 7, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/ajax-file-upload-question/173393/1 "2009-08-07T22:56:28Z")

</div>

I have the file upload working, I have to place it outside my main  
form.

Is there a way so I can have somewhere other than outside my form?

This below is all after the submit button:

 \<%= render :partial =\> '/rsl/sitelist', :locals =\> { :files =\> @files } %\> 
\<% form\_for :file, :url =\> file\_upload\_path(:format =\> 'js'), :html =\> { :multipart =\> true, :target =\> 'upload\_file' } do %\> **File:** &nbsp; \<%= file\_field\_tag :upload %\>&nbsp; \<%= hidden\_field\_tag 'rfi', @rfi.id %\> \<%= submit\_tag 'Upload File' %\>&nbsp; \<% end %\>

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [August 8, 2009, 7:00pm UTC](https://www.ruby-forum.com/t/ajax-file-upload-question/173393/2 "2009-08-08T19:00:02Z")

</div>

anyone?

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [August 10, 2009, 6:46am UTC](https://www.ruby-forum.com/t/ajax-file-upload-question/173393/3 "2009-08-10T06:46:47Z")

</div>

why do you need it outside the main form? is the main form also going  
to be there on that page?  
you might want to check out form\_tag

> **[ActionView::Helpers::FormTagHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001729)**
>
> Provides a number of methods for creating form tags that don't rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.

good luck!

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [August 10, 2009, 7:00am UTC](https://www.ruby-forum.com/t/ajax-file-upload-question/173393/4 "2009-08-10T07:00:15Z")

</div>

Can you be more descriptive?  
If the file is related to the model who’s form is on the page, then the  
file  
can and should be a part of the form for that model.

If the file is unrelated to the model who’s form is in the page and/or  
you  
want to include a separate form for the file upload, then you can use  
something like

\<% form\_tag :file, :url =\> file\_upload\_path(:format =\> ‘js’), :html =\>  
{ :multipart =\> true, :target =\> ‘upload\_file’ } do %\>  
**File:** &nbsp;  
\<%= file\_field\_tag :upload %\>&nbsp;  
\<%= hidden\_field\_tag ‘rfi’, @rfi.id %\>  
\<%= submit\_tag ‘UploadFile’ %\>&nbsp;  
\<% end %\>

… which is just the code u pasted with form\_tag instead of form\_for.  
you  
should read the API to understand the difference.

2009/8/10 Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > > Is there a way so I can have somewhere other than outside my form?  
> > > > **File:** &nbsp;  
> > > > \<%= file\_field\_tag :upload %\>&nbsp;  
> > > > \<%= hidden\_field\_tag ‘rfi’, @rfi.id %\>  
> > > > \<%= submit\_tag ‘UploadFile’ %\>&nbsp;  
> > > > \<% end %\>
> 
> >

–  
In Sport We Trust !!!

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [August 10, 2009, 6:53am UTC](https://www.ruby-forum.com/t/ajax-file-upload-question/173393/5 "2009-08-10T06:53:14Z")

</div>

Well the file upload was not working because it is a form inside a form.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [August 10, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/ajax-file-upload-question/173393/6 "2009-08-10T17:47:06Z")

</div>

I do know the difference but when I move the code into the main form it  
tries to submit the whole form.

On Sun, Aug 9, 2009 at 11:59 PM, Vinay S.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [August 11, 2009, 6:08am UTC](https://www.ruby-forum.com/t/ajax-file-upload-question/173393/7 "2009-08-11T06:08:47Z")

</div>

You can use either form\_for or form\_tag for the file. In either case,  
the  
main form should close before you start the one for the file.  
Whats the problem?

2009/8/10 Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > If the file is unrelated to the model who’s form is in the page and/or you
> > 
> > > > why do you need it outside the main form? is the main form also going
> > > > 
> > > > > On Aug 7, 3:55 pm, Me [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> >

–  
In Sport We Trust !!!

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [August 11, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/ajax-file-upload-question/173393/8 "2009-08-11T16:51:31Z")

</div>

Well all file uploads from what I have seen are within the main form not  
below the submit button. I guess I will just use a modal box to do the  
uploads.

On Mon, Aug 10, 2009 at 11:07 PM, Vinay S.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [August 12, 2009, 5:38am UTC](https://www.ruby-forum.com/t/ajax-file-upload-question/173393/9 "2009-08-12T05:38:59Z")

</div>

I have a Paperclip file upload going inside my model’s form, but not  
in a form for itself.  
I was approaching this as a form issue. Anyway, you should try  
Paperclip if it seems like it might help you. Its really extremely  
easy file uploads.

[www.thoughtbot.com/projects/paperclip](http://www.thoughtbot.com/projects/paperclip)

good luck!

2009/8/11 Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > I do know the difference but when I move the code into the main form it tries to submit the whole form.
> > > 
> > > > ```
> > > > <%= hidden_field_tag 'rfi', @rfi.id %>
> > > > 
> > > > ```
> > > > 
> > > > > > why do you need it outside the main form? is the main form also going
> > > > > > 
> > > > > > > > \<iframe name="upload\_file" id="upload\_file" style="width:1px;height: \</blockquote\> \</blockquote\> \</blockquote\> \</blockquote\> \</blockquote\> \</blockquote\> \<p\>–\<br\> In Sport We Trust !!!\</p\> \</blockquote\> \<blockquote\>\</blockquote\> \</blockquote\> \<p\>–\<br\> In Sport We Trust !!!\</p\>
