如何在ERB文件中交互javascript变量?

时间:2011-07-30 19:43:34

标签: javascript jquery ruby-on-rails

<script type="text/javascript">
  $(document).ready(function() {
    var school_name = $('input#search_school_name_like').val();
  });
</script>

在我的erb文件中,我需要检查,

If school_name is not nil?

    <%= link_to "link" students_path(:range => 'A-B',:school_name => school_name) %>
    else
    <%= link_to "link" students_path(:range => 'A-B) %>

如何在javascript和erb文件之间进行通信?请帮助。

1 个答案:

答案 0 :(得分:0)

Javascript代码在用户的浏览器中执行,ERB文件正在服务器上处理。这意味着你不能在Ruby代码中使用Javascript变量。所以像link_to "link" students_path(:range => 'A-B',:school_name => school_name)这样的代码将不起作用。您必须在Javascript中手动创建路径:

var path = "/students?range=A-B&school_name=" + school_name;