Reputation: 41
I have a form for editing profiles. Rails automatically generates the form id as 'edit_profile_##' where ## is the profile id of the current user(instance variable-@profile_id). I need to use this form id for my javascript functions. Is there a way to get the current user's profile id inside js? Or is there a way I can override the automatic id generation by rails?
Upvotes: 4
Views: 16327
Reputation: 152
lets say you have @user = {'name'=>'steve'}
in the controller
now your html page can render <p> <%=@user['name']%> </p>
now lets say you want to be able to access @user in your .js file; add thisThing & data-url to your tag <p id="thisThing" data-url="<%=@user%>" > <%=@user['name']%> </p>
in your .js file var userInfo = $('#thisThing').data('url')
now you got userInfo in your .js (its in string)
Upvotes: 0
Reputation: 47472
you have to send that using function parameter
.html.erb
<script type="text/javascript">
var user_id = <%= @profile_id %>; // for integer
var user_name = '<%= @profile_name %>'; // for string
abc(user_id)// this is your function in .js file
</script>
.js
function abc(id){
alert(""+id)
}
Upvotes: 9
Reputation: 10392
Are you using normal *.html.erb views?
Can't you do something like :
<script type="text/javascript">
user_id = <%= @profile_id %>;
</script>
?
Upvotes: 1