tags:

views:

42

answers:

1

Hello all, I'm wanting to check a ruby session variable via ajax. I'm familiar with ajax calling php, is calling a ruby file similar?

I'm talking about a 'session[:var_name]' type variable in a Rails environment using JQuery.

Something like:
$.ajax({
url: path to ruby file,
type: "GET",
dataType: "html",
success: function(html){
...

+3  A: 

AJAX requests are not that different from other requests. You might need to change your return type based on what kind of AJAX request you make. If your Javascript is expecting text you can just write an action in a controller like this:

def your_ajax_action
  render :text => session[:var_name].to_s
end

If your Javascript is expecting XML, you need to generate the XML then have an action in your controller like this:

def your_ajax_action
  render :xml => {:var_name => session[:var_name]}.to_xml
end

Or, if you expect JSON:

def your_ajax_action
  render :json => {:var_name => session[:var_name]}.to_json
end
RyanTM