在Rails中,我们应该如何在Java脚本中获取参数?

问题描述:

使用代码时

 <%= render 'chart', width:100 %>

我可以在页面中获取宽度值,例如<%= width%>

I can get the width value in the page, like <%= width %>

但是如果我想在同一页面的Java脚本中获取width值,我真的不知道. 因此,我的未决解决方案是将值隐藏在div中,然后使用$('div').text()检索值.

But if I want to get the width value in java script in the same page, I really have no idea. So my pending solution is hiding the value in the div, then use the $('div').text() to retrieve the value.

所以我只想知道我们是否还有其他方法来获取Java脚本在rails中传递的参数?

So I just want to know if we have any other way to get the parameters passed in rails by java script?

您可以直接在javascript部分中输出值:

You can directly output the value in the javascript part:

<script type="text/javascript">
  var width = <%= width %>;
</script>

在渲染期间 将解释Ruby/Rails中的代码,并将在生成的HTML中输出(红宝石)变量width.

The code in Ruby/Rails will be interpreted during the rendering, and will output the (ruby) variable width inside the generated HTML.