0

Say you have the following code:

# foobars_controller.rb
class FoobarController < ApplicationController
  def new
    @foobar = Foobar.new
    @other_var = 123
    ...
  end
end

# _form.html.erb for foobar
<script>
  $(document).ready(function() {
    $.ajax({
      url: some.route.to.function
      data: {
        other_var: ?????
        ...
      }
    })
  });
</script>

<html>
  <!-- @other_var is visible here, I just need to get it into the AJAX call, above -->
</html>

My question is this: What is the best way to get the value in the controller's @other_var instance variable into the view's AJAX call? I know that instance variables defined in the controller are available in the view files, but I'm not clear how to access that variable from inside the javascript/JQuery.

***Edit:

I see that there are some options for chaining method calls in .ajax, and/or using one AJAX call to get the other_var from the controller, then using another AJAX call to send that value to the original URL of interest. I'll look at ways to make this do what I need.

It still seems like there should be an easier way. The javascript/JQuery is in the view file that has access to the controller instance variables. Is there no way to stuff that variable's value into a form element and then access it there when I need to use it in the AJAX call?

Community
  • 1
  • 1
whognu
  • 439
  • 1
  • 5
  • 15
  • Returning a json document?? – Hackerman Nov 06 '14 at 19:50
  • I don't understand your question. – whognu Nov 06 '14 at 19:51
  • This is your first time using ajax?? – Hackerman Nov 06 '14 at 19:52
  • Pretty close. Definitely the first time in Rails. – whognu Nov 06 '14 at 19:53
  • 1
    That's it...look you can return from the controller a json document with the relevant data....then in the ajax call you have two options to treat the returning json, in the success or in the done call...http://stackoverflow.com/questions/10931836/should-i-use-done-and-fail-for-new-jquery-ajax-code-instead-of-success-and – Hackerman Nov 06 '14 at 19:56
  • So, basically you suggest breaking this into two AJAX calls - the first to the controller to get the variable value, and the second to the main function, using the value retrieved in the first? – whognu Nov 06 '14 at 20:02
  • Not two ajax calls, just one, like this: `$.ajax({ url: some.route.to.function data: { other_var: ????? ... } }).done(function(response){//here response if the data from your controller});` – Hackerman Nov 06 '14 at 20:06
  • Sorry, I'm still not clear exactly how this would work. The done function is executed AFTER the AJAX call, right? I need to get the other_var value BEFORE I send it off with the AJAX call. Also, retrieving the other_var value from the controller requires a different URL than the actual AJAX call I want to make. Wouldn't that mean it needs its own AJAX call? It looks like the done() method just executes a javascript function, not that it goes to a URL for something. – whognu Nov 06 '14 at 20:52

0 Answers0