Need help understanding button_to_function (I think)

This one’s really got me scratching my head.

I’ve got a button_to_function (in application.rhtml) that opens a new window containing a file from the public directory. Or at least that’s what it’s supposed to do. It always launches a new window. Sometimes the file loads as expected. Sometimes the window launches that then I get an ActionController error (in the newly launched window) saying ‘no action responded’ to the file name.

I can reproduce the behavior, but have no idea why it’s behaving the way it is and would really appreciate it if someone could help me understand it.

I’ll post the code below. It’s not very interesting. What is interesting is this.

If the URL just calls the controller with no ‘/’ at the end, the new window is launched and the file opened in it. With the code below, if my URL is ‘http://localhost:3000/admin’ everything works.

OTOH, if my URL is ‘http://localhost:3000/admin/’ or ‘http://localhost:3000/index’ the new window is launched but I get the ActionController error saying ‘no action responded’ to the file name.

I can’t figure out why including the action in the URL, or even just having the ‘/’ at the end of the controller name in the URL, is causing the window.open javascript call to trigger a call to a Rails controller/action.

Anybody? I sure would appreciate any help / explanation.

Thanks!

Bill

--------- controller ------------

class AdminController < ApplicationController

def index
end

end

-------------- index.rhtml ---------------

PDF Created!

<%= button_to_function “View/Print PDF”, “viewPDF()”, :class => ‘submit-btn’ %>

--------- application.rhtml -------------

YourTimeMatters.com <%= javascript_include_tag :defaults %> <%= stylesheet_link_tag 'scaffold' %>

<%= flash[:notice] %>

<%= @content_for_layout %>

This one’s really got me scratching my head. I’ve got a
button_to_function (in application.rhtml) that opens a new window
containing a file from the public directory. Or at least that’s what
it’s supposed to do. It always launches a new window. Sometimes the
file loads as expected. Sometimes the window launches that then I get
an ActionController error (in the newly launched window) saying ‘no
action responded’ to the file name. I can reproduce the behavior, but
have no idea why it’s behaving the way it is and would really appreciate
it if someone could help me understand it. I’ll post the code below.
It’s not very interesting. What *is* interesting is this. If the URL
just calls the controller with no ‘/’ at the end, the new window is
launched and the file opened in it. With the code below, if my URL is
http://localhost:3000/admin’ everything works. OTOH, if my URL is
http://localhost:3000/admin/’ or ‘http://localhost:3000/index’ the new
window is launched but I get the ActionController error saying ‘no
action responded’ to the file name. I can’t figure out why including the
action in the URL, or even just having the ‘/’ at the end of the
controller name in the URL, is causing the window.open javascript call
to trigger a call to a Rails controller/action. Anybody? I sure would
appreciate any help / explanation.

You're javascript is opening "testfile.pdf". What you are seeing is what
I'd expect and has to do with how the browser is going to munge the
current url with what you are opening up since it treats "testfile.pdf" as
relative to the current url....

http://localhost:3000/admin -- this one works because the browser thinks
'admin' is a file since there is no trailing slash. So it drops that and
substitues 'testfile.pdf' resulting in: http://localhost:3000/testfile.pdf

http://localhost:3000/admin/ -- doesn't work because the browser thinks
you are in a directory presumably viewing the index.html (for example) in
that directory so it *doesn't* remove it and simply appends testfile.pdf
so you end up with: http://localhost:3000/admin/testfile.pdf

http://localhost:3000/index -- this one now that I look at it should
work. Be curious to see what your error log says it's trying to load up
when you hit this.

To fix this, change your viewPDF() function to this (with a leading
slash):

     newWindow=window.open("/testfile.pdf","","width=450");

-philip

Hi Philip,

Philip Hallstrom wrote:

What you are seeing is what I'd expect and has
to do with how the browser is going to munge the
current url with what you are opening up since it
treats "testfile.pdf" as relative to the current url....

Thank you for the explanation. I appreciate it very much. I still don't have my arms completely around it, but it sounds like I need to understand more about how requests are passed by the browser. Is that right? Any reading suggestions?

http://localhost:3000/index -- this one now that
I look at it should work. Be curious to see what
your error log says it's trying to load up when you
hit this.

I'd like to figure that out too. What log would I find that in? I'm developing on InstantRails (WinXP)using Mongrel. I couldn't find any log references and so had pretty much no way to debug this.

To fix this, change your viewPDF() function to
this (with a leading slash):

That did it. Thank you again.

Best regards,
Bill