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?