Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access an instance variable inside a Javascript function in Ruby on Rails application?

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?

like image 910
Sathya Avatar asked Jan 21 '23 20:01

Sathya


1 Answers

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)
 }
like image 111
Salil Avatar answered Jan 30 '23 00:01

Salil