2

I'm new in the world of JavaScript, jQuery, and Ajax. I have a JavaScript function where I want to use the value of a JavaScript variable inside of embedded Ruby code. For example, below I want to use the JS p variable as an argument to the Ruby call to @r.search():

$(function (){
  $('#data-select').on('change',function (){
     var p = document.getElementById('data-select').value; 
     alert(<% j @r.search(+ p +) %>);
  });
});

I want to know if i can do this? And if i can what's the syntax?

17
  • Can you explain more? Commented Apr 13, 2015 at 18:50
  • She wants to know if you can put a javascript object inside a ruby function. She is asking if this is valid syntax and it is not. Commented Apr 13, 2015 at 18:52
  • ruby is a server side language and javascript is client side, you're gonna have to think of another way to do what you want. perhaps using ajax Commented Apr 13, 2015 at 18:53
  • 2
    @MZaragoza You can't. It's impossible. You have definitely never done it, because it is impossible. p is a variable declared in JavaScript. You cannot embed it inside Ruby code which has been evaluated long, long before the browser has even started executing JavaScript. Commented Apr 13, 2015 at 19:19
  • 4
    @MZaragoza No, you can't, in ERB or HAML or any Ruby templating languages. You're not understanding the part that is impossible: p is a JavaScript variable. It does not exist at the time the Ruby template is evaluated. p only exists in the user's browser, where there is no such thing as Ruby. p never exists on the server, where Ruby resides. The two can never meet except through requests and responses. You can argue all you like, instead I suggest you try to actually produce a working example that reproduces this. Commented Apr 13, 2015 at 19:21

2 Answers 2

6

First off, I'm assuming this is Ruby on Rails (which you tagged it as). When using ERB you are telling Rails to first evaluate the embedded Ruby code to dynamically insert data that is on the server-side, and then send the file to the client over the internet where their web browser then evaluates the JavaScript.

So if this is what's in your file, and you passed a @user variable with a name method that evaluates to "John Smith":

// test.js.erb
alert("Hello <%= @user.name %>");

When that file is rendered, your server will evaluate it to this:

// test.js
alert("Hello John Smith");

And then send that over the internet to the client. Only once the client's computer has received this file will the JavaScript be evaluated. They (and the JavaScript) will therefore never see the embedded Ruby code. Therefore what you're doing cannot be done the way you have it because the Ruby code is evaluated before the JavaScript can set the p variable.

If you want to get information that resides on the server after the client has received the file and is running the JavaScript, you can use something called Ajax, which uses JavaScript to send data asynchronously back to the server. Check out this Rails guide for more details on that.

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

Comments

1

You Can't do it, because while rendering the view all ERb will be converted to HTML or relevant, so you don't have access tp the Ruby objects after rendering the view.

You can also pass the dynamic values from javascript to certain rails methods

Refer: http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/remote_function

But this rails method also will converted to ajax function after rendering.

The solution:

Try to send an ajax request and show the result in view.

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.