Thursday, March 26, 2009

send_file or send_data in link_to_remote

Hi guys,

really really long time i did anything here :).. and no this time not an usual bhaana(excuse) will do but an genuine reason.. was busy building something good.. and it is a secret(just can't disclose it here like this).

Anyhow coming to point.. i was doing a general routine development task for my company and i got hit by a situation like this.. where i was making an ajax call to a controller/action and in response i was sending a stream i.e (send_data and send_file) but as the post was xmlhttp everything was working fine but download was not available (and yeh over here i coded it for download not the inline stuff). What to do now.... i searched it ya ya googled it and response was not a solution that i can rely on(it might not be google's fault must be mine, but why am i favoring it.. it must be google fault lol :)) )

So i solved it my own way by redirecting the update to other action and coding the stream in the newly redirected action :)

Here are some snippets of it....

<%=link_to_remote "Export the result",:url => {:controller => "some_controller", :action => "ask_to_export"},:with => "'record_ids=1-2-3-4-5-6'" -%>

def ask_to_export
render :update do |page|
page.redirect_to :action => "process_and_export", :record_ids => params[:record_ids]
end
end

def process_and_export
outfile = ""
CSV::Writer.generate(outfile) do |csv|
csv << ["praveen","kumar","sinha",1,2,3,nil,""]
end
send_data outfile, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=report_#{Time.now.to_f.to_s}.csv"
end

I hope this must be helpful to some one stucked over here and googling it

Thanks :)

6 comments:

  1. Hi Praveen,
    Thanks for the tip. It was really helpful for me. I also needed to use the link_to_remote function and was wondering why it wasn't working.

    The next problem I faced was while passing a set of records retrieved from my database, from 'ask_to_export' (according to your example) to 'process_and_export' method.

    if i passed it like

    page.redirect_to :action => "process_and_export", :records => @records

    here @records was the collection of records that were retrieved from my database.

    it really messed up the values in the @records, because they all seem to get converted to this huge string with each value separated by '/'.

    anyway I fixed that by not passing the collection of records retrieved from the database straight into the 'process_and_export' method, but storing the selection scope that I used anywhere withing my controller to retrieve these records in a session variable such as 'session[:selection_scope]'.

    Then I can easily re-retrieve these records from within my 'process_and_export' method by using the 'session[:selection_scope]' whenever it is called.

    Thought to note that down in case someone finds it helpful.
    Cheers!

    ReplyDelete
  2. Thanks. I was trying to do this same thing today, and I couldn't get the download to start. Adding this redirect did the trick.

    ReplyDelete
  3. Thanks it worked for me. I didn't need to put anything on the session as I was only passing an id to the redirected action.

    ReplyDelete
  4. Praveen, thank you for posting this - 2 years later, and it's helped me out :-)

    ReplyDelete