Topic: jQuery getJSON() and reply

Hi all,
I'm using getJSON() function to get data via AJAX.
After the process I get a TemplateMissing error:

ActionView::MissingTemplate (Missing template contracts/myaction with {:locale=>[:it, :it], :formats=>[:json], :handlers=>[:rhtml, :rxml, :builder, :erb, :rjs]} in view paths ...

But I don't want to render a template, I want to return a value to process in the success function:

$.getJSON("myaction/"+level, { }, function(data){
  // I want handle data from ruby here
});

Someone can help me?

Re: jQuery getJSON() and reply

In the controller action that handles the AJAX request,  you'd have something like this:

def yourajaxaction
   @foo = Foo.find(params[:arg])  
   respond_to do |format|  
     format.js  { ... }
    end
end

Whatever is in the block after format.js would be called,  BUT, if you leave it blank , i.e.

def yourajaxaction
  @foo = Foo.find(params[:arg])
  respond_to do |format|  
     format.js  
    end
end

Rails will instead look for a javascript file called yourajaxacton.js.erb and run that.  That javascript file is processed as embedded ruby,  so you can insert, for example, a value from @foo. like this:

$('#idofanelement').html("<p><%= @foo.bar %></p>");
Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: jQuery getJSON() and reply

Thank you! But I still can't get response ...

Console tells me that the request is soing to be processed as JSON:

... Processing by ContractsController#myaction as JSON ...

And I have this format:

respond_to do |format|
  format.json { return "done!" }    
end

I also tryed with format.js but I still get the template error.

Last edited by nimbuz77 (2012-01-19 11:45:33)