如何使 Javascript 警报框显示来自 Rails 控制器的变量 [英] how to make Javascript alert box display a variable from Rails controller

查看:32
本文介绍了如何使 Javascript 警报框显示来自 Rails 控制器的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图文件中有这段 Javascript 代码:

I have this piece of Javascript codes in my view file:

<script>
function hint() {
    var str = @user.name;
    var name = str.toLowerCase();

    alert(name);
}
</script>

我希望警报框显示用户的姓名,因此我使用了在我的控制器中定义的变量 @user.但是,当我单击按钮激活警报框时,没有显示任何内容.我也试过 current_user.name,但也没有用.如何在警告框中显示变量的值?

I want the alert box to display the name of the user so I use the variable @user that was defined in my controller. However, when I click the button to active the alert box, nothing shows up. I also tried current_user.name, but it didn't work either. How can I display the value of a variable in the alert box?

推荐答案

就这样,就像在 html 文件中一样.

Just keep it like this, as like in html file.

 var str = '<%= @user.name %>';

这篇关于如何使 Javascript 警报框显示来自 Rails 控制器的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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