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.
pis 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.pis a JavaScript variable. It does not exist at the time the Ruby template is evaluated.ponly exists in the user's browser, where there is no such thing as Ruby.pnever 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.