Johnny Hall
2007-May-15 08:48 UTC
Problem with RJS and/or Prototype.js =>ResponseText is blank
Hi, I''ve got a real head-scratcher. I''ve got a standard Ajax form using form_remote_tag, which calls a controller action which adds a comment and then sends back some JS via RJS which updates a few items on the page. It did work fine but (and I''ve no idea what has happened) now it doesn''t. It calls the controller action but gets an empty response back and then redirects (it seems) to the controller directly, thus rendering the javascript in plain text. If I get rid of the RJS and render a partial and use :update => "comments" in my form_remote_tag then it does the same thing but renders the html partial. You can check it out by going to the site itself at http://indecipherable.co.uk/home/index/welcome-to-indecipherable/ Click add comment to show the form and off you go. The server is mongrel (on my dev box as well, though it does the same with webrick). I''ve looked at Firebug and the request goes off ok but no responseText comes back. The RJS code looks like this page.visual_effect "BlindUp", "add-comment" page.insert_html :top, "comments", :partial => "/common/comments/comment", :object => @comment page.show "show-comment" page.replace_html "comment-count", pluralize(@comment.entry.comments.count, "Comment") page[:latest].visual_effect :highlight, :startcolor => "#ffffff" #, :endcolor => "#b1d0e9" The controller action looks like this def comment @comment = Comment.new(:text => params[:text]) @comment.user = User.find(:first) @comment.entry = Entry.find_by_identifier(params[:entry]) if (@comment.save) @latest_comment = @comment # render :partial => "/common/comments/comment", :object => @comment # This fails too else render :action => "show" end end The form looks like this <% form_remote_tag(:url => { :action => :comment }, :html => {:class => "margin-form" }) do %> <label for="text"> Your Comment: <%= text_area_tag(:text, "", :rows => 4, :cols => 50) %> </label> <label> <%= submit_tag("add your comment »", :class => "submit", :disable_with => "Sending comment…") %> </label> <% end %> I''ve searched high and low and found a few similar stories but every time the reason for the odd behaviour hasn''t been related to my code, which seems to be pretty boilerplate to me. Thanks for any help. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Johnny Hall
2007-May-15 08:49 UTC
Re: Problem with RJS and/or Prototype.js =>ResponseText is b
Here is the header (after the page has refreshed to .../comment) - nothing unusual (to my eyes). Response Headers Cache-Control no-cache Connection Keep-Alive Date Tue, 15 May 2007 08:31:50 GMT Content-Type text/javascript; charset=utf-8 Server WEBrick/1.3.1 (Ruby/1.8.6/2007-03-13) Content-Length 1213 Set-Cookie _indecipherable_session_id=e6e8e71a0fa0bbef590ef22814a64126; path=/ Request Headers Host localhost:3000 User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3 Accept text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language en-gb,en;q=0.5 Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 300 Connection keep-alive Referer http://localhost:3000/home/index/welcome-to-indecipherable/ Cookie style=null; _indecipherable_session_id=e6e8e71a0fa0bbef590ef22814a64126 --- The only before_filter that runs populates a couple of globals with some navigation data from the db. If I stop it from running for the comment action, it makes no difference. --- Here''s the (only) log entry from the action Processing ContentController#comment (for 127.0.0.1 at 2007-05-15 09:37:12) [POST] Session ID: e6e8e71a0fa0bbef590ef22814a64126 Parameters: {"text"=>"...", "action"=>"comment", "controller"=>"content", "entry"=>"welcome-to-indecipherable", "page"=>"index", "section"=>"home"} [4;36;1mComment Columns (0.000000) [0;1mSHOW FIELDS FROM comments [4;35;1mUser Load (0.000000) SELECT * FROM users LIMIT 1 [4;36;1mUser Columns (0.016000) [0;1mSHOW FIELDS FROM users has_many_polymorphs: INIT [4;35;1mEntry Columns (0.016000) SHOW FIELDS FROM entries [4;36;1mTagging Columns (0.016000) [0;1mSHOW FIELDS FROM taggings has_many_polymorphs: injecting dependencies [4;35;1mEntry Load (0.015000) SELECT * FROM entries WHERE (entries.`identifier` = ''welcome-to-indecipherable'') LIMIT 1 [4;36;1mSQL (0.000000) [0;1mBEGIN [4;35;1mSQL (0.000000) INSERT INTO comments (`entry_id`, `text`, `user_id`, `created_at`) VALUES(2, ''...'', 1, ''2007-05-15 09:37:13'') [4;36;1mSQL (0.016000) [0;1mCOMMIT Rendering content/comment Rendered /common/comments/_comment (0.00000) [4;35;1mSQL (0.000000) SELECT count(*) AS count_all FROM comments WHERE (comments.entry_id = 2) Completed in 0.74900 (1 reqs/sec) | Rendering: 0.01600 (2%) | DB: 0.07900 (10%) | 200 OK [http://localhost/home/index/welcome-to-indecipherable/comment] So, it''s only running the action once, rather than once for the AJAX call and then again for the regular browser refresh. It looks like the AJAX call is getting no reply or not getting to it''s destination and then it falls through to POST the form in the normal fashion. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Roelofs
2007-May-15 12:58 UTC
Re: Problem with RJS and/or Prototype.js =>ResponseText is blank
Well when I post a comment, Firebug shows me an error, but the page is redirected too fast for me to see what the error actually is. Manually running the RJS on the comments page seems to work quite well. You''ll need to find a way to keep the page from redirecting into the RJS to find out what the problem is. Jason On 5/15/07, Johnny Hall <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hi, I''ve got a real head-scratcher. I''ve got a standard Ajax form using > form_remote_tag, which calls a controller action which adds a comment > and then sends back some JS via RJS which updates a few items on the > page. > > It did work fine but (and I''ve no idea what has happened) now it > doesn''t. It calls the controller action but gets an empty response back > and then redirects (it seems) to the controller directly, thus rendering > the javascript in plain text. > > If I get rid of the RJS and render a partial and use :update => > "comments" in my form_remote_tag then it does the same thing but renders > the html partial. > > You can check it out by going to the site itself at > > http://indecipherable.co.uk/home/index/welcome-to-indecipherable/ > > Click add comment to show the form and off you go. > > The server is mongrel (on my dev box as well, though it does the same > with webrick). > > I''ve looked at Firebug and the request goes off ok but no responseText > comes back. > > The RJS code looks like this > > page.visual_effect "BlindUp", "add-comment" > page.insert_html :top, "comments", :partial => > "/common/comments/comment", :object => @comment > page.show "show-comment" > page.replace_html "comment-count", > pluralize(@comment.entry.comments.count, "Comment") > page[:latest].visual_effect :highlight, :startcolor => "#ffffff" #, > :endcolor => "#b1d0e9" > > The controller action looks like this > > def comment > @comment = Comment.new(:text => params[:text]) > @comment.user = User.find(:first) > @comment.entry = Entry.find_by_identifier(params[:entry]) > if (@comment.save) > @latest_comment = @comment > # render :partial => "/common/comments/comment", :object => @comment > # This fails too > else > render :action => "show" > end > end > > The form looks like this > > <% form_remote_tag(:url => { :action => :comment }, > :html => {:class => "margin-form" }) do %> > <label for="text"> > Your Comment: > <%= text_area_tag(:text, "", :rows => 4, :cols => 50) %> > </label> > <label> > <%= submit_tag("add your comment »", :class => "submit", > :disable_with => "Sending comment…") %> > </label> > <% end %> > > I''ve searched high and low and found a few similar stories but every > time the reason for the odd behaviour hasn''t been related to my code, > which seems to be pretty boilerplate to me. > > Thanks for any help. > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Johnny Hall
2007-May-15 15:07 UTC
Re: Problem with RJS and/or Prototype.js =>ResponseText is b
Jason Roelofs wrote:> Well when I post a comment, Firebug shows me an error, but the page is > redirected too fast for me to see what the error actually is. > > Manually running the RJS on the comments page seems to work quite well. > > You''ll need to find a way to keep the page from redirecting into the RJS > to > find out what the problem is. > > JasonThanks. Yep, the script itself is fine. I''ve been living in Firebug, stepping through prototype.js for the last day and I can''t for the life of me work it out. The call goes through to the controller but no response comes back. I don''t see an error in Firebug and it doesn''t break if I set "break on all errors". The controller action obviously works because it does send the JS back to the browser. The JS works, as we''ve seen. The controller action doesn''t even fire for the AJAX call (checking the log files). I can''t see where it calls the controller action with a "manual" POST - is that by design in prototype? Is there something that needs to be set or switched on, or otherwise, to allow RJS to work? I''m sure that whatever it is, it''s something obvious/simple/annoying. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Johnny Hall
2007-May-15 15:27 UTC
Re: Problem with RJS and/or Prototype.js =>ResponseText is b
Ok. I''ve discovered something I was wrong about above. It does make 2 calls to the comment action - one for AJAX and one for the POST. I changed the controller code to add server-side degradability, which has the effect of making the page work properly (I check request.xhr?). Now, the log shows 2 entries and 2 entries are posted. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
a bad job -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---