1

I want to pass my local variable to escape_javascript like this:

function renderPartial(partial) {
          $('body').append('<%=escape_javascript render(:partial => partial) %>');
}

But I got an error: undefined local variable or method `partial' for #<#:0x00007f9be09a36b0>

So my question is how I can pass my local variable in the case.

2 Answers 2

1

The code in <% %> brackets is executed by ERB preprocessor on server side. It's Ruby code. As I see your variable partial is an argument of JS function, it will work on client side. So these 2 variables are from different realms. And local Ruby variable partial is not defined.

Please explain what you want to achieve with this code?

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

2 Comments

I see. I want to render partial depend on action of user. And I don’t want to add many of if else.
I'm afraid you'll have to do it, with if else or switch. If you want to render your partial by calling JS method, you'll need to pre-render them on server side and pass to a client as raw HTML code. The other option (better) is instead of rendering make an AJAX call to the server which will return rendered partial.
0

Can you just change partial variable name, I think partial is reserve keyword in rails.

Comments

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.