如何在 Rails 视图中访问 javascript 变量 [英] how to access javascript variable in rails view
本文介绍了如何在 Rails 视图中访问 javascript 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以访问 rails 视图中的 javasript 变量.我只是设置了 javascript 变量的一些值,并想在 Rails 视图中访问它.
Is there any way to access javasript variable inside the rails view. I just set some value of javascript variable and want to access it in rails view.
谢谢
推荐答案
您必须了解视图中的 ruby 代码会在服务器上执行 - 在页面上的任何 javascript 执行更改之前.
You have to understand that ruby code in your views gets executed on the server - before any javascript on the page gets a change to be executed.
这就是为什么你不能做这样的事情:
That's why you cannot do stuff like this:
<script>
var js_var = 1;
</script>
<%= get_value_of_js_var_somehow %>
反之亦然:
<script>
var js_var = <% generate_value_with_ruby %>;
do_something_in_javascript_with_js_var();
</script>
这篇关于如何在 Rails 视图中访问 javascript 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文