2

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?

1
  • 1
    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. Commented Jul 27, 2012 at 8:18

4 Answers 4

2

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)

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

3 Comments

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?
@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
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() %>'
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.

Comments

1

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.

2 Comments

You can have javascript on the server (node.js) and ruby on the client (if a client is a standalone app, not web browser) :)
even like this you can not call functions from the other side without using tcp/ip communication, still funny, troll?
0

Well you can have js.erb file. In which you can include ruby code. Just take a look at this code snippet from my project.

$("#myModal").modal('hide');
<% if @status == "ok" %>
  <%= add_gritter("We will contact you as soon as possible!", :image => :success, :class_name => "gritter-light") %>
<% else %>
  <%= add_gritter("Something went wrong while saving you request. Please try again!", :image => :error, :class_name => "gritter-light") %>
<% end %>

Comments

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.