# Link\_to with callbacks?

**URL:** <https://www.ruby-forum.com/t/link-to-with-callbacks/89777>\
**Category:** Rails\
**Created:** [March 22, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/link-to-with-callbacks/89777 "2007-03-22T18:25:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 22, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/link-to-with-callbacks/89777/1 "2007-03-22T18:25:33Z")

</div>

I’ve got an image link that’s currently using link\_to\_remote. The link  
calls a method that creates a file and completes with render :nothing =\>  
true. I’m using the :loading and :complete callbacks on  
link\_to\_remote. On :loading I call a function that renders a  
‘disabled’ image while the file’s being created. When the method  
completes, I use the :complete callback to call another function that  
opens a popup containing the file that got created and then renders the  
original ‘enabled’ image.

The above works fine except for one thing. AFAIK, because it’s being  
loaded by as opposed to being sent to the browser, the file that gets  
rendered in the popup has to exist in the ‘public’ (or some other  
externally visible) directory. I need the file to _not_ be visible  
externally.

The first solution I tried was to eliminate the popup (too many folks  
have popups disabled anyway) and change the ‘render :nothing =\> true’ to  
a ‘send\_file’. That sends the file to the browser (I can see the file  
content in Firebug) but, I guess since the browser is expecting a JS  
response, it doesn’t do anything with it. Anybody know a way to send a  
file using RJS so I can get the ‘view or save’ dialog box?

The second solution I tried was to change the link\_to\_remote to link\_to.  
That lets me send the file from a non-externally-visible location, which  
solves that problem, but AFAIK link\_to doesn’t support the callbacks. I  
guess I could use the :onclick to trigger rendering the ‘disabled’  
image, but can’t figure out how I’d be able to change back to the  
‘enabled’ image once the file was received. Any thoughts on how I could  
make this work?

Any thoughts on other approaches I might try would be greatly  
appreciated. Like, is there maybe some way to use routes to have the  
window.open(‘title’, ‘location/filename’) being executed in the browser  
to open the popup and retrieve the file be intercepted by Rails and  
mapped to a hidden location?

Thanks,  
Bill
