在 Rails 中,您可以这样做:
<% name = @user.name %>
<script>
var name = <%= name%>;
alert(name);
//prints the name of the user
</script>
你可以反过来做,像这样
<% name = @user.name %>
<script>
var name = "John";
<% name %> = name;
</script>
<% @user.name = name %>
<%= name %>
<!-- prints the name of the user, in this case "John" -->
显然这不起作用,但出于示例的目的
关键是,您可以将 javascript 变量的值传递给 rails 中的 ruby 变量吗