简体   繁体   English

在javascript HAML中将javascript变量传递给ruby函数调用

[英]passing javascript variable to ruby function call in javascript HAML

I'm using Sinatra, Ruby, HAML, and JavaScript 我正在使用Sinatra,Ruby,HAML和JavaScript

So I figured out how to call a ruby function in HAML JavaScript 所以我想出了如何在HAML JavaScript中调用ruby函数

@@layout
%html
    %head
        %title #{@title}
          :javascript
              var retval
              function diff_by_company_id() {
                 retval = "#{ruby_function()}";
              }

However I now need to pass the ruby function a javascript variable and I'm not sure how do it. 但是,我现在需要将ruby函数传递给一个javascript变量,但我不确定该怎么做。 This is what I tried that obviously doesn't work. 这是我尝试过的,显然不起作用。

def test(input)
    return "Output Number: "+input.to_s + "!"
end

@@layout
%html
    %head
        %title #{@title}
          :javascript
              var retval
              function diff_by_company_id() {
                 var input = 1;
                 retval = "#{ruby_function("input")}";
              }

instead of passing the value of input, 1, it passes in the string input. 而不传递输入值1,而是传递字符串输入。

So what i get is: retval = Output Number: input! 所以我得到的是: retval = Output Number: input!

What I want is: retval = Output Number: 1! 我想要的是: retval = Output Number: 1!

Any help on how to pass this correctly would be appreciated. 任何有关如何正确通过此方法的帮助将不胜感激。

Ruby executes on the server side, javascript executes on the client side. Ruby在服务器端执行,而javascript在客户端执行。 The reason this is working now is that the ruby code is executed when the page is loaded. 现在起作用的原因是在加载页面时执行了ruby代码。

It is not possible to "pass a javascript variable to ruby" without making an AJAX request to the server. 如果不向服务器发出AJAX请求,则不可能“将javascript变量传递给ruby”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM