# 2 button\_to functions for file downloads

**URL:** <https://www.ruby-forum.com/t/2-button-to-functions-for-file-downloads/160137>\
**Category:** Rails\
**Created:** [February 12, 2009, 4:37am UTC](https://www.ruby-forum.com/t/2-button-to-functions-for-file-downloads/160137 "2009-02-12T04:37:04Z")\
**Posts on this page:** 8\
**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:** [February 12, 2009, 4:37am UTC](https://www.ruby-forum.com/t/2-button-to-functions-for-file-downloads/160137/1 "2009-02-12T04:37:04Z")

</div>

I have 2 button\_to functions that download 2 different files. the  
first one acts like a submit and the second on actually does downlaod  
the file. Does anyone know what would cause that?

\<%= button\_to ‘Download PCCIS file’, :action =\> ‘downloadpccis’ %\>  
\<%= button\_to ‘Download CIS file’, :action =\> ‘downloadcis’ %\>

---

<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:** [February 13, 2009, 2:29am UTC](https://www.ruby-forum.com/t/2-button-to-functions-for-file-downloads/160137/2 "2009-02-13T02:29:44Z")

</div>

Anyone have any idea why one button does a submit and the other does a  
file download?

---

<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:** [February 13, 2009, 4:25am UTC](https://www.ruby-forum.com/t/2-button-to-functions-for-file-downloads/160137/3 "2009-02-13T04:25:36Z")

</div>

On Thu, 2009-02-12 at 17:29 -0800, Me wrote:

> Anyone have any idea why one button does a submit and  
> the other does a file download?

In general, element behavior in any page is a function of the browser  
parser’s ability to make sense of the code we feed it. Post your code  
and we might be able to help.

Best regards,  
Bill

---

<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:** [February 13, 2009, 5:27am UTC](https://www.ruby-forum.com/t/2-button-to-functions-for-file-downloads/160137/4 "2009-02-13T05:27:41Z")

</div>

For some reason the first button is giving me this for a response:  
try {  
$(“notification”).update(“

# Site 123 was built
”);  
$(“downloads”).show();  
} catch (e) { alert(‘RJS error:\n\n’ + e.toString()); alert(’$  
(“notification”).update("\<h1 style=‘display: inline’ class='mes  
‘\>Site 123 was built");\n$(“downloads”).show();’); throw e }

---

<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:** [February 13, 2009, 4:46am UTC](https://www.ruby-forum.com/t/2-button-to-functions-for-file-downloads/160137/5 "2009-02-13T04:46:04Z")

</div>

Application.rhtml:  
\<% form\_tag( {:action =\> ‘mltn6p’} ) do -%\>

# Wendia Site entry Program

 Select your Shelf \<%= select(:node, :id, $shelves,{:prompt =\>'Select Equipment'},{ :onchange =\> remote\_function(:url =\> { :action =\> "gotoshelf"})} ) %\>   
 \<%= render :partial =\> 'sitedata' %\> Download POB Files \<%= button\_to 'Download CIS file', :action =\> 'downloadcis' %\> \<%= button\_to 'Download PCCIS file', :action =\> 'downloadpccis' %\> 

```
<div id="content">
    <div id="notification"> </div>
        <%= yield %>
</div>

```

\<% end %\>

controller:  
def downloadcis  
send\_file("#{$site.upcase}\_CIS.csv", :type =\> ‘text/plain’,  
:disposition =\> ‘attachment’)  
end

```
def downloadpccis
    send_file("#{$site.upcase}_PCCIS.csv", :type => 'text/plain',

```

:disposition =\> ‘attachment’)  
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:** [February 14, 2009, 1:22am UTC](https://www.ruby-forum.com/t/2-button-to-functions-for-file-downloads/160137/6 "2009-02-14T01:22:36Z")

</div>

Anyone have any ideas why the second button works but the first button  
is giving me the above error?

---

<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:** [February 14, 2009, 6:55pm UTC](https://www.ruby-forum.com/t/2-button-to-functions-for-file-downloads/160137/7 "2009-02-14T18:55:26Z")

</div>

OK Well this must be a bug I guess but I managed to get it to work.

For some reason the first one was trying to give the js response to  
the action. So for the hey of it I used 3 button\_to’s and the bottom  
two worked while the first one was giving me the same response.

\<%= button\_to 'Download PCCIS file', :action =\> 'downloadpccis' %\>
\<%= button\_to 'Download PCCIS file', :action =\> 'downloadcis' %\> \<%= button\_to 'Download PCCIS file', :action =\> 'downloadpccis' %\>

---

<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:** [February 14, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/2-button-to-functions-for-file-downloads/160137/8 "2009-02-14T18:59:32Z")

</div>

On Feb 14, 5:54 pm, Me [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> OK Well this must be a bug I guess but I managed to get it to work.

button\_to generates a form for you with a button. You’ve put a button  
in a form, therefore in the generated html there will be a form nested  
inside a form which isn’t valid html (and also sorts of weird stuff  
can happen, like the “wrong” form being generated).

Fred
