RJS Templates not conducting callbacks

I’ve run across a weird problem with RJS that I’m trying to figure out:

I have an application that was running 0.13.1 that I recently upgraded
to Rails 1.0. I wanted to use RJS templates, so I installed the plugin
and updated my prototype javascript file via ‘rake update_prototype.’

I attempted to test the templates out via the following code, but the
AJAX callbacks did not get processed when the action was completed. No
errors occurred (Javascript errors or application errors from the
log). The update simply does not occur.

When I build a new Rails application and install the plugin, the
templates work as advertised. They just don’t work with the existing
app.

class TestController < ApplicationController

The test view is rendered via this action

def index
end

This action updates the view via an RJS template

def test
end
end

views/test/index.rhtml:

<%= javascript_include_tag :defaults %>

replace me
<%= link_to_remote 'link', :url => {:action => 'test'} %>

views/test/test/rjs:

page.replace_html ‘test’, ‘hello’

I’m struggling to figure out why they work in the new application and
not in the existing one - I would think the parts of a rails app that
could impact RJS would be the version of Rails installed, the version
of the plugin installed, and the version of prototype. However, as
stated earlier, all of these are the same (unless I made a dumb
mistake).

Anyone have any ideas why everything seems to work but the most
important part, the callbacks?

Anyone have any ideas why everything seems to work but the most
important part, the callbacks?

What is the rendered result of this?

<%= javascript_include_tag :defaults %>

replace me
<%= link_to_remote 'link', :url => {:action => 'test'} %>

I suspect it’s not evaluating the incoming JavaScript from the RJS
templates.

-Ben

Ben,

This is the rendered result:

Element.update(“test”, “hello”);

So it’s the correct javascript, but like you said, it’s just not being
evaluated.

  • Derek

On 12/31/05, Benjamin S. [email protected] wrote:

templates.

-Ben


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA
Keeping it Simple.
404.593.4879

This is the rendered result:

Element.update(“test”, “hello”);

Wrong part; I was referring to the page which calls the action in the
first place. (e.g. the result of link_to_remote)

-Ben

Ben,

It’s the same code for each link_to_remote:

Non working:
link

Working:
link

  • Derek

On 12/31/05, Benjamin S. [email protected] wrote:

[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA
Keeping it Simple.
404.593.4879

Non working:
link

Working:
link

Those both look the same; I assume evalScripts is false in the first
one?

This is all really puzzling me, since Rails 1.0 is all ready out of
the box except for the changes in the plugin.

-Ben

Ben,

Yeah - I’m clueless here…it looks like I need to get my hands on
something to debug AJAX callbacks so I can see if anything is actually
being returned to the page.

  • Derek

On 12/31/05, Benjamin S. [email protected] wrote:

This is all really puzzling me, since Rails 1.0 is all ready out of
the box except for the changes in the plugin.

-Ben


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA
Keeping it Simple.
404.593.4879

Hi, Derek,

I run to the similar problem as you, however, I found the my problem was
caused by that the returned html code from rjs were not escaped from my
old application code.

If I do a view source directly in firefox, I can’t see the difference.
However, if I use fireforx extension “view rendered source chart”, I can
see
that for the working one, the html was escaped.

Not sure if your case is the same.

Tony

Derek H. wrote:

Ben,

It’s the same code for each link_to_remote:

Non working:
link

Working:
link

  • Derek

On 12/31/05, Benjamin S. [email protected] wrote:

[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA
Keeping it Simple.
404.593.4879

Hi Derek,

A few things for you to try. First make sure prototype.js is included
in your page header ie view source. Also make sure it’s the same script
as for your newer app.

Second add a puts “Hello” in your rjs template to make sure it is
actually getting called. Should show in the console. It’s ruby code
right !!

Third I am assuming this is a typo but is your template called
view/test/test.rjs ?

Let us know…

Hi, Derek,

I found the cause of my problem now.

When I directly access the rjs action in IE, the broken one returned
some
html, which is exactly the return result of the rjs. However, for the
working
one, it will popup a window for downloading and eventually an error
message
will popup. This reminds me that the Content-Type for the broken one and
the
working one should be different.

When looking at the RJS code in revised “base.rb”
from http://dev.rubyonrails.org/changeset/3078, line 398 of the revised
file:

@controller.headers[‘Content-Type’] ||= ‘text/javascript’\n” +

which showed that the Content-Type for rjs returned result has been
changed
to “text/javascript”, that’s why the generated js can be automatically
evaluated.

In my old application, I have overwrote the Content-Type of http header
to make
utf-8 as default character set in the applicaton.rb (for i18n), using
before_filter. The Content-Type was set to “text/html; charset=utf-8”,
like this:

 @headers["Content-Type"] = "text/html; charset=utf-8"

This makes the Content-Type of returning result from rjs become
text/html,
which obviously can’t be evaluated. However, if you view the content of
the
generated js code, you can’t tell the difference.

To solve this problem, I changed the before filter to after filter to
let
rails grab the original Content-Type for specific controller first, then
add charset value after the Content-Type. Like this:

 content_type = @headers["Content-Type"]
 @headers["Content-Type"] = "#{content_type}; charset=utf-8"

And it worked. I guess your case might be similar, hopefully this will
help

-Tony

Tony J. wrote:

Hi, Derek,

I run to the similar problem as you, however, I found the my problem was
caused by that the returned html code from rjs were not escaped from my
old application code.

If I do a view source directly in firefox, I can’t see the difference.
However, if I use fireforx extension “view rendered source chart”, I can
see
that for the working one, the html was escaped.

Not sure if your case is the same.

Tony