0

I'm trying to use js var inside ruby line (which all are inside a js.erb file):

my_file.js.erb:

var x = 3;
var credit = "<%= CreditsController.some_method(x) %>";

I know how to replace text in js var, but don't know how to do it before running the ruby part <%= %>

Thanks!

2
  • 3
    You can't do this that way. You can't use client-side's language variables on server. Commented Oct 14, 2014 at 8:21
  • You could use Ajax to request data from the server. Commented Oct 14, 2014 at 8:24

2 Answers 2

2

That's not possible because your file my_file.js.erb will be processed into my_file.js on the server side replacing every ruby code with their outputs it provides. Finally, your navigator will execute the javascript.

As Stefan said, you could try fetching that value by an ajax call, or trying to implement that function in the javascript, if it is possible.

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

Comments

0

what you can do is something like:

in your CreditsController:

def some_method
  @credit = 0
end

and in your some_method.js.erb:

var credit = "<%= @credit %>";

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.