Hi friends,
How to use javascript variables in ruby.
i have activity id on a_id
function(a_id)
{
<%@foo=Activity.where("activity_id = ? ", a_id)
}
how can solve it
Hi friends,
How to use javascript variables in ruby.
i have activity id on a_id
function(a_id)
{
<%@foo=Activity.where("activity_id = ? ", a_id)
}
how can solve it
Name it: app/assets/javascript/file.js.erb and it will go through ERB.
Actually I think I'm wrong try:
http://railscasts.com/episodes/324-passing-data-to-javascript?view=asciicast
It's not at all clear to me what you are asking, but remember:
- Ruby runs on the server;
- Javascript runs in the browser.
- Sometimes Ruby on the server is used to create/modify Javascript to be run in the browser.
hi
here i have mention example code
hi
here i have mention example code
<script type="text/javascript">
var js_name="daya";
<%=@user=User.where("name=?", js_name%>
alert("<%=@user%>");
</script >
If you're going to use Erb in your JavaScript, then you must name your file whatever.js.erb. Then your <%= %> will expand to be whatever the local value of @user is at the moment that script is requested. You will need a controller, too, and your request will need to have some context around it. If your controller had a respond_to block in it, and your JavaScript file was named sensibly for the controller method, then this would Just Work™,
respond_to do |format|
format.js { render :layout => false }
format.html
end
Walter
OK, evaluate that code in light of what I said previously:
- Ruby runs on the server;
- Javascript runs in the browser.
Now:
1) What do you think that code will do???
2) What are you actually trying to accomplish?
Remember the above is evaluated on the /server/ before the page is
rendered. js_name is not meaningful at that time.
Colin