0

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

could someone provide a code example as I have about 20 min of ajax experience.
@Richard you'd be better off telling us why you want to do what you're doing. What's the aim not the implementation.
i am trying to send user input through a ruby function and display the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.