1

This seems really simple but I can't find a straightforward answer to this problem anywhere. Basically, I have a JavaScript application that is sitting on a server and it needs to pass information back and forth between the JS application and the server, which is built on Rails. First, the JS application needs to tell the server to execute a function that generates said values, and then it needs a way to get them back to the JS application so it can use them, and they communicate back and forth like this during any given session of the application.

Is there any easy way to do this? I realize Ajax is somehow involved but I can't figure out how.

Ryan Faulhaber
  • 76
  • 1
  • 3
  • 7
  • The function JavaScript needs to tell the server to execute is just generating a few integer values. – Ryan Faulhaber May 27 '15 at 17:45
  • From the web page, you use Javascript to make an Ajax call to your Rails controller. The controller maps the request to a route and to your ruby code that handles the request. That code returns the value which becomes the return value from the Ajax call which your client-side Javascript receives and can then do something with. There are tons of articles written on this topic on the web. A search for "Javascript Ajax Rails" yields a lot of reading material. Here's one such article: http://guides.rubyonrails.org/working_with_javascript_in_rails.html – jfriend00 May 27 '15 at 17:58
  • @RyanFaulhaber Your question is very similar to this question[1]. Have you tried the solution provided in the same thread? Just curious. Also, Railscasts are always your good friend[2] [1] http://stackoverflow.com/questions/5933261/how-to-call-a-rails-method-from-in-jquery [2] http://railscasts.com/episodes?utf8=%E2%9C%93&search=ajax – Rahul Roy May 27 '15 at 18:02
  • This question is probably too broad for Stackoverflow because this requires a fairly wide ranging tutorial on both client-side Ajax and handling Ajax requests in Rails. – jfriend00 May 27 '15 at 18:09
  • @RahulRoy I have tried the solution but the line `$.ajax("/users/render_read")` generates a 404 error even when I set everything up similarly. – Ryan Faulhaber May 27 '15 at 18:13
  • @jfriend00 yeah fair enough, that is becoming evident. – Ryan Faulhaber May 27 '15 at 18:14

1 Answers1

0

You are right that the answer lies in ajax. Assuming you're using jQuery, you will need to submit a request to the server like so:

                $.ajax({ 
                    type: "GET", 
                    url: '/get_info_action',  
                    data: {some_data: some_value}, 
                    dataType: "script" 
                });

Make sure the value you choose for url is set up in your routes.rb file. There is a good Railscast about this sort of thing that helped me a lot when learning how to do this: http://railscasts.com/episodes/136-jquery-ajax-revised

rockusbacchus
  • 1,257
  • 1
  • 12
  • 11
  • 1
    You should mention that your ajax example requires the jQuery library. jQuery is convenient to use for Ajax, but is not required. – jfriend00 May 27 '15 at 18:07
  • @jfriend00 good catch, thanks for the heads up. It's been so long since I wrote ajax calls without jQuery that I didn't even realize I was making an assumption. – rockusbacchus Jun 01 '15 at 02:16