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 --------------- <h1>PDF Created!</h1> <%= button_to_function "View/Print PDF", "viewPDF()", :class => ''submit-btn'' %> --------- application.rhtml ------------- <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>YourTimeMatters.com</title> <%= javascript_include_tag :defaults %> <%= stylesheet_link_tag ''scaffold'' %> <script type="text/javascript"> function viewPDF() { newWindow=window.open("testfile.pdf","","width=450"); newWindow.focus } </script> </head> <body> <p style="color: green"><%= flash[:notice] %></p> <%= @content_for_layout %> </body> </html> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2006-Nov-17 03:52 UTC
Re: 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.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> Thanks! > Bill > --------- controller ------------ > class AdminController < ApplicationController > > def index > end > > end > -------------- index.rhtml --------------- > <h1>PDF Created!</h1> > <%= button_to_function "View/Print PDF", "viewPDF()", :class => ''submit-btn'' %> > --------- application.rhtml ------------- > <html> > <head> > <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> > <title>YourTimeMatters.com</title> > <%= javascript_include_tag :defaults %> > <%= stylesheet_link_tag ''scaffold'' %> > <script type="text/javascript"> > function viewPDF() { > newWindow=window.open("testfile.pdf","","width=450"); > newWindow.focus > } > </script> > </head> > <body> > <p style="color: green"><%= flash[:notice] %></p> > <%= @content_for_layout %> > </body> > </html> >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bill Walton
2006-Nov-17 17:38 UTC
Re: Need help understanding button_to_function (I think)
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---