Forum: Ruby on Rails RJS Templates not conducting callbacks

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Derek H. (Guest)
on 2005-12-31 08:06
(Received via mailing list)
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 %>
<div id="test">replace me</div>
<%= 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?

- Derek
--
Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA
Keeping it Simple.
404.593.4879
Benjamin S. (Guest)
on 2005-12-31 08:34
(Received via mailing list)
> 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 %>
> <div id="test">replace me</div>
> <%= link_to_remote 'link', :url => {:action => 'test'} %>

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

-Ben
Derek H. (Guest)
on 2005-12-31 08:44
(Received via mailing list)
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. <removed_email_address@domain.invalid> wrote:
> templates.
>
> -Ben
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA
Keeping it Simple.
404.593.4879
Benjamin S. (Guest)
on 2005-12-31 08:50
(Received via mailing list)
> 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
Derek H. (Guest)
on 2005-12-31 10:26
(Received via mailing list)
Ben,

It's the same code for each link_to_remote:

Non working:
<a href="#" onclick="new Ajax.Request('/test/test',
{asynchronous:true, evalScripts:true}); return false;">link</a>

Working:
<a href="#" onclick="new Ajax.Request('/test/test',
{asynchronous:true, evalScripts:true}); return false;">link</a>

- Derek

On 12/31/05, Benjamin S. <removed_email_address@domain.invalid> wrote:
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA
Keeping it Simple.
404.593.4879
Benjamin S. (Guest)
on 2005-12-31 19:50
(Received via mailing list)
> Non working:
> <a href="#" onclick="new Ajax.Request('/test/test',
> {asynchronous:true, evalScripts:true}); return false;">link</a>
>
> Working:
> <a href="#" onclick="new Ajax.Request('/test/test',
> {asynchronous:true, evalScripts:true}); return false;">link</a>

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
Derek H. (Guest)
on 2005-12-31 21:02
(Received via mailing list)
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. <removed_email_address@domain.invalid> 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
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Derek H.
HighGroove Studios - http://www.highgroove.com
Atlanta, GA
Keeping it Simple.
404.593.4879
Tony J. (Guest)
on 2006-02-08 16:53
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:
> <a href="#" onclick="new Ajax.Request('/test/test',
> {asynchronous:true, evalScripts:true}); return false;">link</a>
>
> Working:
> <a href="#" onclick="new Ajax.Request('/test/test',
> {asynchronous:true, evalScripts:true}); return false;">link</a>
>
> - Derek
>
> On 12/31/05, Benjamin S. <removed_email_address@domain.invalid> wrote:
>> removed_email_address@domain.invalid
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>
> --
> Derek H.
> HighGroove Studios - http://www.highgroove.com
> Atlanta, GA
> Keeping it Simple.
> 404.593.4879
De R. (Guest)
on 2006-02-08 17:40
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...
Tony J. (Guest)
on 2006-02-08 22:47
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
>
This topic is locked and can not be replied to.