Ship It! LIVEShip It! LIVE
home about services writing contact

We develop, test, and create fine software products, and design creative solutions to your problems.
The development of software is an intrinsically creative process. We are dedicated to improving our mastery of the art.
Links · RSS Feed
Popular Pages

Quoting Watts Humphrey, "Developers are caught in a victim's mentality." We never think it's our fault, it's always somebody else's.
-Jared Richardson
Ship It! is part manual of best practices, part software methodologies book and part a distillation of ideas and experiences of good and bad projects that the authors have been involved in. It migh...
-Tech Book Report
...It would be really nice if, as an industry, we could stop being such a bunch of screwed-up clowns and start living up to our potential. Ship It! is one of the things that could help, if only tho...
-Mike Gunderloy

Problems with RJS (May 30)
I've had this problem repeatedly with RJS, and I'm not saying it's not something that I'm doing... in fact, if I had to take a bet on whether the problem is in my code or the RJS code, I'd bet it's my code. :)

Never the less, I've seen several pages online where this has been an issue for other people and it keeps coming back to haunt me, so here's my fix.

When you're using RJS to do JavaScript magic and you add something simple like

page.replace "main", :partial => "update_student"

and the JavaScript just shows up on the page you're trying to update, then you've found this issue. You see something like this in the browser:

try {
Element.replace("main", "
\r\n

\r\n Your updates have been recorded\r\n

\r\n
"); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.replace(\"main\", \"
\\r\\n

\\r\\n Your updates have been recorded\\r\\n

\\r\\n
\");'); throw e }

The problem is that the RJS libraries didn't include the SCRIPT tag. I think it gets confused and thinks the tag is already in place.

The fix is pretty simple... add the SCRIPT tag. In RJS you can always add raw JavaScript to the page with the

 << 
operator. So I changed the RJS template above to read like this:


page << ' <SCRIPT type="text/javascript">'
page.replace "main", :partial => "update_student"
page << ' </SCRIPT>'

And it all works the way I think it should... which is to say, maybe not quite the way the authors intended for it to be used, but it seems to be working for me so far....

So, if you're using RJS and the JavaScript shows up on the page, just add an opening and closing set of SCRIPT tags before and after the RJS call in question.

Category: Rails

Agile Tour (2010-10-28)
This will be a great collection of speakers on Agile and business topics.
Indie Conf (2010-11-13)
I'll be talking about keeping your skills current at this conference for independent web developers
Agile Dev Practices East (2010-11-14)
This world-class conference in Orlando always has a great mix of speakers and attendees. Don't miss it!


© 2007 Agile Artisans.