# How can I get instance variable return from ajax on ruby

**URL:** <https://www.ruby-forum.com/t/how-can-i-get-instance-variable-return-from-ajax-on-ruby/117752>\
**Category:** Rails\
**Created:** [October 19, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/how-can-i-get-instance-variable-return-from-ajax-on-ruby/117752 "2007-10-19T22:40:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [October 19, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/how-can-i-get-instance-variable-return-from-ajax-on-ruby/117752/1 "2007-10-19T22:40:13Z")

</div>

in AJAX, there is responseText, responseBody, responseXML,  
responseStream,  
so I don’t know what I can use to get instance variable return from  
ajax on ruby…  
who can tell me, how can I do. Thanks!!

---

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [October 20, 2007, 12:06am UTC](https://www.ruby-forum.com/t/how-can-i-get-instance-variable-return-from-ajax-on-ruby/117752/2 "2007-10-20T00:06:45Z")

</div>

OnRails wrote:

> in AJAX, there is responseText, responseBody, responseXML,  
> responseStream,  
> so I don’t know what I can use to get instance variable return from  
> ajax on ruby…

That depends on what you want to do with your instance variable. The  
easiest way is probably to use an RJS template.

But first, some perspective…

See, the question isn’t really “how can I get at this instance  
variable,” it’s, what do I do with the data given to me from my Ajax  
request?

And the answer to that question depends on what format the data is in  
and what JavaScript framework you’re using.

If you’re using Prototype, try using an RJS template first. Here’s an  
example (from memory, so it might not be 100% accurate):

def my\_action  
@my\_var = MyObject.find(:first)

render :update do |page|  
page.replace\_html(‘some\_dom\_id’, @my\_var.some\_attribute)  
end  
end

The render :update directive tells your application to render JavaScript  
to be executed by the browser. RJS stands for Ruby JavaScript (I think)  
and is basically like writing JavaScript with Ruby code. You have access  
to a lot of Prototype’s functionality from within these templates, and  
you don’t have to write any JavaScript.

See the doc on Rails’ JavaScript generator:  
[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html)

Good luck!

---

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [October 20, 2007, 1:54am UTC](https://www.ruby-forum.com/t/how-can-i-get-instance-variable-return-from-ajax-on-ruby/117752/3 "2007-10-20T01:54:32Z")

</div>

Thanks Daniel.  
in my case, I just want to get @searchresult.

In controller:  
search\_controller  
…  
def index  
…some codes.  
end

def search  
@searchresult=Search.find(:all)  
some other codes.  
end

in views:  
search.rthml

 aa bb cc
here just display @searchresult \<====juse display @searchresult (1)

when I used AJAX, that means I changed selected, I got all the changed  
content, include index and search, displayed (1)  
so I don’t know what I can use to get instance variable return from  
ajax. and I don’t want to make XML file for this case. Just only want  
to get @searchresult  
to change view…
