I'm using Sinatra, Ruby, HAML, and JavaScript
So I figured out how to call a ruby function in HAML JavaScript
@@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. 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.
So what i get is: retval = Output Number: input!
What I want is: 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. The reason this is working now is that the ruby code is executed when the page is loaded.
It is not possible to "pass a javascript variable to ruby" without making an AJAX request to the server.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.