如何在 Ruby on Rails 应用程序中访问 Javascript 函数内的实例变量? [英] How to access an instance variable inside a Javascript function in Ruby on Rails application?

查看:46
本文介绍了如何在 Ruby on Rails 应用程序中访问 Javascript 函数内的实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于编辑个人资料的表格.Rails 自动生成表单 ID 为edit_profile_##",其中 ## 是当前用户的配置文件 ID(实例变量-@profile_id).我需要将此表单 ID 用于我的 javascript 函数.有没有办法在js中获取当前用户的个人资料ID?或者有什么方法可以覆盖 Rails 的自动 id 生成?

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?

推荐答案

你必须使用函数参数发送那个

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)
 }

这篇关于如何在 Ruby on Rails 应用程序中访问 Javascript 函数内的实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆