1

How to use ruby and javascript mixing?

For example:

<div>
    $(".pop-up-confirm").click( function(e) {
      var domain_name = $("input#domain_name")[0].value;
      var msg = #{call_ruby_function(domain_name)};
    }
</div>

First, I use js to get the value domain_name from input. Second, I want call ruby_function, use domain_name as parameter. The first step is correct, but I how to make the second correct.

1
  • Normally what you want to do is have some data attribute with some url and in javascript you would perform ajax request to that url. Response body would be your message. Commented Apr 8, 2016 at 12:43

2 Answers 2

3

You cannot execute ruby in the browser. You can only execute javascript. You can either rewrite your function in javascript, or make a request to your ruby server.

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

1 Comment

You are right and I will make a request to ruby server. Thank you for your help!
1

To solve your issue you should perform AJAX request.

$.get('/some-path', { domain: domain_name }, function(data) {
  # data is your message
});

Ruby code will be executed in server-side and your message should be returned to you to work with in client-side

My example requires jQuery to be available

1 Comment

This is now my way. Thank you for your help!

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.