4

I have a javascript function which takes rails value like

<body onload="myFunction(<%p%>)">

and I'm trying to pass a value coming from the controller like this

<% if logged_in? -%><%p= current_user.id %><% end -%> but here the p doesn't contain the value. this works if I put code like this
<body onload="myFunction(<%= current_user.id%>)">
<% if logged_in? -%><%= current_user.id %><% end -%>

But I need a further requirement, so another line like this should be add

<% if logged_out? -%><%p= current_user.id %><% end -%>

So if the user is logged out then the value p will passed to the javascript function. I know this is a small thing but don't know how to do it, I'm new to ror so please help and if you have tutorials to learn this point me too, so that can I save the time of both you and me in the future.

2 Answers 2

1

Hope it will help:

Passing Data to JavaScript

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

Comments

0

Please use like this when assign any value

      <body onload="myFunction('<%=p%>')">

Hope work it

6 Comments

another thing ,is this right <%p= current_user.id %> kashif?
but this <body onload="myFunction('<%=p%>')"> is not working..:(
But i thing better is you first set the id of user into p variable like this p = current_user.id and then pass p variable to function
Sorry for disturbing you, do you know any ebook or tutorials to learn these things?
Rails guide is best also micheal hurt tutorial is best.But js you will learn with passage of time.
|

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.