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?
views:
39answers:
2
+1
A:
Are you using normal *.html.erb views?
Can't you do something like :
<script type="text/javascript">
user_id = <%= @profile_id %>;
</script>
?
Francisco Soto
2010-07-01 04:40:44
yes i do use html.erb views. So u mean to say i can include this in my html.erb file? but how do i access user_id inside my .js file?
Sathya
2010-07-01 04:59:01
I have no idea how you have your js files. But you can create methods and then pass the parameter around, or create a global variable (like in my example, though I really would not recommend that).
Francisco Soto
2010-07-01 05:37:38
+1
A:
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)
}
Salil
2010-07-01 05:01:19
ok thanks. so thats the only way then? i was trying to see if there was a way without passing it in a function call. coz my js function (see below-submitProfileForm) is actually an event handler which needs to be called only when the form is submittedEvent.observe('window','load',function(){$('edit_profile_##').observe('submit',submitProfileForm);});
Sathya
2010-07-01 05:10:01