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?

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

10 Comments

All excellent points. Unfortunately, even with corrected syntax the code doesn't interpolate, instead rendering: "#{escape_javascript(render :conversations => new)}"
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 #{ ... }
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.
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.
What is the error? not really sure how template rendering will work together with the assets pipeline. Update: looks like bad news: github.com/rails/rails/issues/1370
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.