The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
RJS update and redirects

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Maxim Kulkin

Posts: 58
Nickname: hapk
Registered: Sep, 2006

Maxim Kulkin is developer in Selectosa Systems.
RJS update and redirects Posted: Sep 10, 2006 7:02 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Maxim Kulkin.
Original Post: RJS update and redirects
Feed Title: Software development
Feed URL: http://maximkulkin.blogspot.com/feeds/posts/full?alt=rss
Feed Description: Software development
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Maxim Kulkin
Latest Posts From Software development

Advertisement
Recently I've ran into problem with processing redirects with Ajax.
First solution that I've found was using custom status code handler in link_to_remote (and some other functions), like this:


<% link_to_remote "Foo",
:url => { :action => "foo" },
302 => "document.location = request.getResponseHeader('location')" %>

But I've got problems with it:
1. It didn't work for me
2. I find it rather harassing to add 302 => .... to every link_to_remote function (which sometimes are hidden deep inside helper methods)

Second attempt to solve this problem was using RJS redirect_to:


render :update do |page|
page.redirect_to '/some/url'
end

This worked fine but had some unwanted side-effects: when redirect is rendered it appeared on a page inside updated element while browser was preparing to do the redirect.
After some investigation I've found out that the javascript was sent with content-type of 'text/javascript' and plain text (without any <script> tag surrounding). That's why after Prototype's Ajax.Updater inserted it into update element it showed up. Then I dug some Prototype's sources and found out that it can strip <script> tags and evaluates everything inside it (if needed). So, my solution was to alter ActionController::Base.render_javascript
to surround javascript code with <script> tags if it was Ajax request.

Here is the code:


module ActionController
class Base
def render_javascript(javascript, status = nil) #:nodoc:
if @request.xhr?
render_text("<script type="'text/javascript'">#{javascript}</script>", status)
else
@response.headers['Content-Type'] = 'text/javascript; charset=UTF-8'
render_text(javascript, status)
end
end
end
end




I found out that content-type recongnition is implemented in Prototype v1.5something... So there is no need to patch render_javascript anymore...

Just be sure to run "rake rails:update:javascripts"

Read: RJS update and redirects

Topic: Danes On Rails Previous Topic   Next Topic Topic: render_tree for Rails

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use