0

I have a lightbox in which I want to render a Rails partial, like so:

var data = "render :conversations => new";
$('#lightbox').append('#{escape_javascript(data)');

Unfortunately, this results in the text "render :conversations => new" appearing in the lightbox, rather than the actual rendering. I assume this is because I'm appending the partial rather than rendering it at runtime. I've followed the advice on this thread, but to little success. It recommends I add a "!=" in front of the append line like so:

!= "$('#lightbox').append('#{escape_javascript(data)');"

This throws a syntax error because of the "!=". Perhaps I'm using this symbol incorrectly, but I can't find any information about it on Google. How might I get this to work?

Community
  • 1
  • 1
nullnullnull
  • 8,039
  • 12
  • 55
  • 107

1 Answers1

2

I think you want to do:

$('#lightbox').append('#{escape_javascript(render :conversations => new)');

But I don't really understand how your code could result in appending the text "render :conversations => new". var data = "render :conversations => new"; looks like JavaScript and escape_javascript(data) is server side ruby so it could not know about data. I notice also that the the #{ has no ending }. Is this really the exact code you tested?

Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57
  • All excellent points. Unfortunately, even with corrected syntax the code doesn't interpolate, instead rendering: "#{escape_javascript(render :conversations => new)}" – nullnullnull Jul 19 '12 at 19:54
  • 1
    Ah ok but was it "#{escape_javascript(data)}" before then? is this in a static js file or a rails erb template etc? if a template i think you should use the erb syntax `<%= ... %>` instead of ruby string interpolation `#{ ... }` – Mattias Wadman Jul 19 '12 at 20:05
  • Unfortunately, it's in a static js file--and a fairly hefty one at that. (It handles all aspects of the lightbox.) I do have it in the assets pipeline, but I'm assuming that won't help with patching in the erb syntax. – nullnullnull Jul 19 '12 at 20:09
  • Ah, I think this was the tip I needed! I changed the js file to js.erb, and it seems to be working. I'm getting some other errors, but at least it seems to be interpolating now. – nullnullnull Jul 19 '12 at 20:12
  • What is the error? not really sure how template rendering will work together with the assets pipeline. Update: looks like bad news: https://github.com/rails/rails/issues/1370 – Mattias Wadman Jul 19 '12 at 20:39
  • I think you need to render the partial in a view instead, maybe pass the rendered text as argument to some JavaScript function? – Mattias Wadman Jul 19 '12 at 20:44
  • Yep, that's the problem. I'm researching wasy to get the javascript out of the assets pipeline, short of cutting and pasting it directly into the view. I'll update you once I find an answer. – nullnullnull Jul 19 '12 at 20:54
  • You could do something like this: in the static js file `function show_lightbox(html) { blablabla... append(html); }` and then in the view you render it like this: `show` etc – Mattias Wadman Jul 19 '12 at 20:59
  • Clever trick! It works, though I wonder how well it performs on slower machines, as it's passing a pretty big chunk of code. I was also able to get [this method](http://stackoverflow.com/a/3438707/1255372) to work. – nullnullnull Jul 19 '12 at 21:35
  • Cool. I don't i will be that heavy, i would guess the heaviest part will be to parse the html code each time the lightbox i shown... but as always, just try if it really is slow :) – Mattias Wadman Jul 19 '12 at 21:57