Serving file from AJAX call

I have a simple query/filter widget that via AJAX call updates a results

:results> Search ....
<div id="results> ..table of results </div> <p>When the user enters a value into the search field, clicks submit the<br> ajax does it magic, the controller is called and new results are updated<br> to the results div. All very happy and working well.</p> <p>However inside the filter widget now want another submit button<br> “download to excel” that will pick up the search input values and rather<br> than update the results div with the values, push out a formated table<br> as a spread sheet.</p> <p>In the controller I create a text string and then send it to the<br> browser.<br> mytext = render_to_string( :action => ‘list_excel’, :layout =><br> false)<br> send_data mytext, :type => ‘application/vnd.ms-excel’,<br> :disposition => ‘inline’</p> <p>I created a “submit_to_remote” link to call a different controller<br> method that will push out the excel</p> <p><%= submit_to_remote(“excel”, “excel”,<br> :url => { :action => :push_excel<br> :update => :results },<br> :method => ‘post’ ) %></p> <p>Everything is called correctly but it doesnt work, excel is not called,<br> the reason is that the html headers are not updated thus IE doest know<br> to start excel.</p> <p>How do I inside a AJAX form_remote_tag construct, create a button that<br> when clicked will do the following:<br> a) The controller will have the params of the form available to modify<br> the results(ie the search filter value), I can get excel start with a<br> link_to a different controller but the values of the form are not<br> available (or I dont know how to access or include them in a link_to<br> call)<br> b) Popup, render or send_data or any way to send a full page with the<br> correct headers to have a browser start excel.</p> <p>I hope my question is not to confusing.</p>

Glenn C. wrote:

How do I inside a AJAX form_remote_tag construct, create a button that
when clicked will do the following:
a) The controller will have the params of the form available to modify
the results(ie the search filter value), I can get excel start with a
link_to a different controller but the values of the form are not
available (or I dont know how to access or include them in a link_to
call)
b) Popup, render or send_data or any way to send a full page with the
correct headers to have a browser start excel.

I hope my question is not to confusing.

I dont think you can download a file via javascript. I think your best
bet is to render an iframe with the url of your spreadsheet file in it.
The browser should save thefile or launch the appropriate application
when it tries to read the data.

Glenn C. wrote:

How do I inside a AJAX form_remote_tag construct, create a button that
when clicked will do the following:
a) The controller will have the params of the form available to modify
the results(ie the search filter value), I can get excel start with a
link_to a different controller but the values of the form are not
available (or I dont know how to access or include them in a link_to
call)
b) Popup, render or send_data or any way to send a full page with the
correct headers to have a browser start excel.

Change the form to a normal form, with its target parameter optionally
set to “_blank”, and change the AJAX submit to submit_to_remote.


We develop, watch us RoR, in numbers too big to ignore.

Glenn C. wrote:

Mark Reginald J. wrote:

Change the form to a normal form, with its target parameter optionally
set to “_blank”, and change the AJAX submit to submit_to_remote.

With the submit_to_remote what is the :update target? I just cannot see
how the html header will be refreshed?

It’s the AJAX search that should be a submit_to_remote. The Excel button
should instead be a normal submit button:

<%= form_tag({:action => ‘search_excel’}, :target => ‘_blank’) %>
<%= text_field_tag ‘terms’ %>
<%= submit_to_remote nil, ‘Search’, :url => {:action => ‘search’},
:update => :results %>
<%= submit_tag ‘Search and display results in Excel’ %>

..table of results


We develop, watch us RoR, in numbers too big to ignore.

Mark Reginald J. wrote:

Glenn C. wrote:

How do I inside a AJAX form_remote_tag construct, create a button that
when clicked will do the following:
a) The controller will have the params of the form available to modify
the results(ie the search filter value), I can get excel start with a
link_to a different controller but the values of the form are not
available (or I dont know how to access or include them in a link_to
call)
b) Popup, render or send_data or any way to send a full page with the
correct headers to have a browser start excel.

Change the form to a normal form, with its target parameter optionally
set to “_blank”, and change the AJAX submit to submit_to_remote.


We develop, watch us RoR, in numbers too big to ignore.

With the submit_to_remote what is the :update target? I just cannot see
how the html header will be refreshed?

Mark Reginald J. wrote:

Glenn C. wrote:

Mark Reginald J. wrote:

Change the form to a normal form, with its target parameter optionally
set to “_blank”, and change the AJAX submit to submit_to_remote.

With the submit_to_remote what is the :update target? I just cannot see
how the html header will be refreshed?

It’s the AJAX search that should be a submit_to_remote. The Excel button
should instead be a normal submit button:

<%= form_tag({:action => ‘search_excel’}, :target => ‘_blank’) %>
<%= text_field_tag ‘terms’ %>
<%= submit_to_remote nil, ‘Search’, :url => {:action => ‘search’},
:update => :results %>
<%= submit_tag ‘Search and display results in Excel’ %>

..table of results


We develop, watch us RoR, in numbers too big to ignore.

Mark well, it works, works well!!! I spent close to 10 hours on a very
complex javascript solution for this problem (and failed), it always
amazes me how elegant ROR solutions are, if only I had the intelligence
to think of the solution myself!

Rather than a submit_to_remote, I combined everything with an
form_observer I have the complete functionality set I wanted to deliver

3 gold “Genius” stars to Mark

OK I found one issue with the user interface, should the user hit return
in the filter wizard (form) he/she will popup a excel sheet and for them
this is misterious because they didnt click on the excel button?

How can I disable posting if user hits the enter key?

Glenn C. wrote:

OK I found one issue with the user interface, should the user hit return
in the filter wizard (form) he/she will popup a excel sheet and for them
this is misterious because they didnt click on the excel button?

How can I disable posting if user hits the enter key?

Yeah, that’s a bit of fly in the ointment. Here’s one way to
switch the enter key from Excel to AJAX search:

<%= form_tag({:action => ‘search_excel’}, :target => ‘_blank’) %>
<%= text_field_tag ‘terms’, nil,
:onkeypress => “return event.keyCode != 13 ||
($(‘s’).click(),false)” %>
<%= submit_to_remote nil, ‘Search’, :url => {:action => ‘search’},
:update => :results, :html => {:id
=> ‘s’} %>
<%= submit_tag ‘Search and display results in Excel’ %>


We develop, watch us RoR, in numbers too big to ignore.