How can I get instance variable return from ajax on ruby

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!!

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

Good luck!

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…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs