call ruby function from a java script function

12,598

Solution 1

The Implementation you need depends on when are you calling the ruby code or function

  1. Is is while building or rendering a page or js?

    Then you can you use the js with .erb extension .(as suggested by Super engineer) This allows you to call any ruby code and function available in views and application helpers.

    eg demo.js.erb

    var arr = "<%= call_function %>"
    
  2. You need the function from client side? Then it is impossible to call the function directly, You need to use ajax for such scenarios. (as suggested by Spernsky Danil)

Solution 2

I think it is not possible, there are some reasons:

  1. Browsers don't support Ruby
  2. JavaScript and Ruby have different system of types and I don't know any interface between them
  3. Also you cannot place JavaScript code and Ruby code in one file, because there is no mime type for such file

So I suppose you should convert your Ruby function to JavaScript funciton.

Or you may implement your Ruby function as a part of the Server API and call it from JavaScript by Ajax.

Solution 3

JavaScript is a client-side language and Ruby is a server-side language, they can't call each other directly, use AJAX or WS instead for communicating between server and client sides.

Share:
12,598
Suyog Sakegaonkar
Author by

Suyog Sakegaonkar

Updated on June 05, 2022

Comments

  • Suyog Sakegaonkar
    Suyog Sakegaonkar almost 2 years

    I have written a ruby function and want to place it in js file, the js function should be able to give a call to ruby function and store the return value.

    Is it possible? How to do this task?

    • Aditya Sanghi
      Aditya Sanghi almost 12 years
      Javascript is a runtime environment on the browser (client side). I assume that your server side environment is Rails which is a different runtime environment (server side). The client makes requests to the server side using the HTTP protocol. When you say "call ruby from a javascript function" what you are essentially wanting to do is "make an AJAX call from the browser to do a database operation on the server side". Once you understand this interaction, you will be able to search the right place and get appropriately relevant answers IMHO.
  • Sergio Tulentsev
    Sergio Tulentsev almost 12 years
    You can have javascript on the server (node.js) and ruby on the client (if a client is a standalone app, not web browser) :)
  • micnic
    micnic almost 12 years
    even like this you can not call functions from the other side without using tcp/ip communication, still funny, troll?
  • Max Rose-Collins
    Max Rose-Collins over 10 years
    I know I am reviving an old thread. you wrote <%= call_function %> where is this call_function located? Say I wanted to call a controllers method, how would I do this?
  • Pritesh Jain
    Pritesh Jain over 10 years
    @MaxRose-Collins it can be any valid function that can be accessed by view can be in helpers or anywhere but should be accessible in views
  • Max Rose-Collins
    Max Rose-Collins over 10 years
    Okay, so if I have a helper in application_helper, I can call that from a js.erb file located in assets/javascripts? like this, var response = '<%= solve() %>'