AJAX to Partial?

I'm trying to create a div that doesn't get populated with data until an AJAX call gets sent and then it's shown to the user (previously is hidden). Unfortunately, I'm fairly new to all of this and am still trying to figure out how to do things the "Rails" way.

I was thinking that I could just do a link_to_remote and have the controller render the partial that has my content. However, all JavaScript in my partial gets stripped out and so it doesn't function properly. I could probably include the JavaScript in the main view, but that'd defeat the purpose of using AJAX to load it later (attempting to have less load at once and therefore have less strain on the server and client at once).

I did see some mentions on here about how to include JavaScript in a rendered partial, but I didn't quite understand it all and figured that I'd first ask and see if I'm going about it the right way before I spent a bunch of time trying to figure that all out. Or, am I barking up the wrong tree with link_to_remote and doing a render :partial in the first place?

Thanks for your help, Charlie

Aragorn450 wrote:

I was thinking that I could just do a link_to_remote and have the controller render the partial that has my content. However, all JavaScript in my partial gets stripped out and so it doesn't function properly.

You gotta post some code. Rendering with JavaScript happens all the time around here, but one typo with render :update will twist you JS...

Well, that could well be my problem since I'm not currently using render :update, but rather render :partial in the controller. Let me try switching to that (once I figure out how to use it properly) and re-post with those results...

But for reference, here's what I'm currently doing:

Inside my view I've got

<%= javascript_include_tag 'prototype' %> <%= link_to_remote( 'Show Graph',                          :update => 'graph',                          :url =>{ :action => 'graph',                                   :id => params[:id] }) %>

Then inside my controller I have:

  def graph     get_tests     get_data     render :partial => 'graph'   end

The code comes up fairly quickly, but I get a JavaScript error saying that PlotKit is not defined. When I check the rendered source code, it shows that all of the JavaScript is missing that was in the graph partial (app/views/report/_graph.rhtml).

However, I had noticed references to render :update and so I'm thinking that maybe I should go that way. Once I convert my code to that (have another, more pressing, project that I'm doing right now), I'll post my results.

Thanks Again, Charlie

Aragorn450 wrote:

But for reference, here's what I'm currently doing:

Inside my view I've got

<%= javascript_include_tag 'prototype' %> <%= link_to_remote( 'Show Graph',                         :update => 'graph',                         :url =>{ :action => 'graph',                                  :id => params[:id] }) %>

Then inside my controller I have:

def graph    get_tests    get_data    render :partial => 'graph' end

The code comes up fairly quickly, but I get a JavaScript error saying that PlotKit is not defined. When I check the rendered source code, it shows that all of the JavaScript is missing that was in the graph partial (app/views/report/_graph.rhtml).

How did you get this source? View Source on a browser won't show you the effects of any JavaScript. The best bet would be to use more JavaScript to access document.body.innerHTML. Watir would be great for that.

However, I had noticed references to render :update and so I'm thinking that maybe I should go that way. Once I convert my code to that (have another, more pressing, project that I'm doing right now), I'll post my results.

Render :update do |page| page.stuff end only works on local RJS. Your _graph.rhtml should work; show some of it.

And what's PlotKit? Why not use Gruff or GnuPlot? The more you do on the server the better...

How did you get this source? View Source on a browser won't show you the effects of any JavaScript. The best bet would be to use more JavaScript to access document.body.innerHTML. Watir would be great for that.

The Web Developer toolbar for Firefox lets you view generated source (View Source-->View Generated Source from the toolbar). Great tool for making sure your generated JavaScript source actually works properly...

Render :update do |page| page.stuff end only works on local RJS. Your _graph.rhtml should work; show some of it.

And what's PlotKit? Why not use Gruff or GnuPlot? The more you do on the server the better...

You can get to my _graph.rhtml at http://betablue.net/_graph.rhtml. Every JavaScript piece in there (included .js files and the script itself at the end) is just deleted from the file when I do a render on it. And yes, the code is ugly, but it does work because I had it directly inside the view earlier and it only stopped working after I pulled it out...

As for PlotKit, it's actually rather necessary because we are going to be running this off of Solaris 2.7 machines and I really don't want to mess with installing ImageMagick (or other C libraries) for those... I will get Gruff working eventually for the Linux machines (much easier), but for now, I don't want to deal with the Solaris and ImageMagick.

Aragorn450 wrote:

You can get to my _graph.rhtml at http://betablue.net/_graph.rhtml.

I hope someone here can spot the bug in all that!

As for PlotKit, it's actually rather necessary because we are going to be running this off of Solaris 2.7 machines and I really don't want to mess with installing ImageMagick (or other C libraries) for those... I will get Gruff working eventually for the Linux machines (much easier), but for now, I don't want to deal with the Solaris and ImageMagick.

Install gnuplot and grab gnuplot.rb. Much easier than RMagick's peculiar issues...

The less you do in the browser the better!!!

Hi, I just took a quick look at the file in questions and it seems that you have a few opening tags with the following:

<%-

This should be '<%=' if you would like that code evaluted by the ruby interpreter.

Good luck,

-Conrad

Conrad Taylor wrote:

<%-

This should be '<%=' if you would like that code evaluted by the ruby interpreter.

It's always evaluated; you mean he needs it inserted into the page.

Good catch. And if you must write so much in <%%>, push it into Helper modules and just call into it from the RHTML...

Agreed, I should push some of that into helpers, but the <%- -%> is always evaluated and is the same as <% %> (doesn't output to the browser), except that the new line before and the new line after are removed (doesn't put extra new-lines into your source code like <% %> does).

However, I don't think that the lack of helpers is what is causing the issue... I finished my other project, so I'll be dedicating tomorrow (Friday) to this and post on here if I figure it out.

Thanks for your help! Charlie

Aragorn450 wrote:

Agreed, I should push some of that into helpers, but the <%- -%> is always evaluated and is the same as <% %> (doesn't output to the browser), except that the new line before and the new line after are removed (doesn't put extra new-lines into your source code like <% %> does).

However, I don't think that the lack of helpers is what is causing the issue... I finished my other project, so I'll be dedicating tomorrow (Friday) to this and post on here if I figure it out.

You didn't address the suggestion - shouldn't some <%%> be <%= %>?

You didn't address the suggestion - shouldn't some <%%> be <%= %>?

No, all of the <%- -%> should be parsed without any output (it's interchangeable with <% %>) and all of the <%= %> pairs are doing output like they should.

Granted, some of the <%- -%> pairs that I have could be converted to helpers, but that wouldn't effect the end result (that I know of... am I missing something else here?).

I have all of my latest code located at www.betablue.net/reports_app/

Everything should be fairly self-explanatory, but the list.rhtml is my view that I'm doing the AJAX call from. list.rhtml.direct_partial is a version without the AJAX link which just renders the graph directly (using render :partial => 'graph') and the two HTML files are the rendered result of each version of list.rhtml (AJAX and a direct render :partial).

Obviously, the HTML won't work on this public server since I don't have rails running on this one, but you can at least see what I'm talking about with the JavaScript being stripped.

I might get rails running on an external server (it's internal right now) this weekend and post that link if I'm still having problems.

Thanks again for everybody's help so far. I've really appreciated all your suggestions.

Charlie

Well, just so you guys know, I figured out what my problem was, but it's quite odd...

In list.rhtml, I was using the following to create the link that I was using to update the page:

<%= javascript_include_tag 'prototype' %> <%= link_to_remote('Show Graph',                    :update => 'main_graph',                    :url =>{:action => 'graph',                            :id => params[:id] }) %>

Well, it appears that the generated JavaScript from that is what was removing the JavaScript from my _graph.rhtml file. I stopped using the above code and switched to MochiKit's AJAX implementation and it started working fine.

I'm going to do some more testing because it doesn't make much sense to me, but that's what it looks like anyway...

Thanks again for all your help! Charlie

Yes, that's correct.

-Conrad