hey Max, Mike has a good point. Many of us in Rails land use jquery. While I'm using Rails 2.0.2, I started working with jquery back in 1.0.x. Back then you had to explicitly ask jquery to send json.... the rails respond_to block expects the content-type and expects headers to be set to work. I know things have changed since then and all the work around logic I built in is no longer needed, but I haven't spent the time figuring out what is cruft or what is still needed.
here are some things to look for: * put a breakpoint (or at least a print statement...oh, did I just say that? ;) into the various sections of your respond to block... and see what rails is rendering. * in rails land, format.json and format.js in your respond_to block are not exactly the same thing. I would recommend keeping json calls defined as json in jquery (see below) and all other ajax calls as js. * when you call your ajax, make sure you specify 'json' as the dataType. In an older version of jquery this didn't do the trick for rails..and this is where i"m not sure if they fixed it or not. BUT, if that doesn't work, try adding a 'beforeSend' function in your jquery ajax call that does something like this: function(dataType) { // we want to update the logout timer becaue the user just made an action... SweetSpot.logout.update_timer(); if ( dataType == 'script' || dataType == 'json' ){ return function(xhr){ xhr.setRequestHeader("Accept", "text/javascript, text/ html,application/xml, text/xml, */*"); }; } if ( dataType == 'xml' ) { return function(xhr){ xhr.setRequestHeader("Accept", "application/xml, text/xml, text/html, text/javascript */*"); }; } } on a rails note (and this is a personal observation!), but your respond_to block could probably be simplified (and you have a bug when processing errored xml with json... the @json object will be blank when it hits that else statement, so calling #errors on it will throw an exception, returning a 500) @json = property.self_and_children_to_json respond_to do |format| format.json{ render :json => @json} format.html{ render :nothing => true, :status => unprocessable_entity } end good luck! Adam -- Adam Greene SweetSpot.dm -- Diabetes Wellness for the Family http://www.SweetSpot.dm http://blog.SweetSpot.dm On May 7, 9:32 am, "Michael Geary" <[EMAIL PROTECTED]> wrote: > Ah, very good, Max, you're a step ahead of me on the debugging. > > Since you've narrowed it down to a Rails issue, would it make more sense to > ask on a Rails list? Don't get me wrong, there are a number of Rails experts > here who would probably be happy to help, it's just that you might get more > focused assistance that way. > > -Mike > > > From: Max Williams (Brighton) > > > Hi Mike, thanks for your advice. In answer: > > > 1) The JSON is valid (it was generated using rails .to_json > > method and that site you recommended says it's valid) > > > 2) I'm pretty sure a JSON download will work with JS/jQuery, > > because the async treeview example uses a php script to > > return some json - see > >http://jquery.bassistance.de/treeview/demo/async.html, look > > at the 'server component used' to see the php in question. > > > 3) See 2. > > > I'm confident that the json/jquery side of things is all > > cool. My problem is purely rails-specific: how do i ask a > > rails controller for a specific chunk of json, and how do i > > send it back? Or, in other words, how do i manage in rails > > what the example uses a php script for? > > > thanks again > > > On May 7, 4:57 pm, "Michael Geary" <[EMAIL PROTECTED]> wrote: > > > It sounds like you're trying to debug three things at once: > > > > * Can I generate valid JSON from myRailsapp? > > > > * Can I get a JSON download to work with JavaScript and jQuery? > > > > * Can I getasyncJSON to work withtreeview? > > > > I would simplfy the debugging problem by separating those three > > > questions and tackling them one at a time. That will make it a lot > > > easier to track down the problem. > > > > For example, you can test the first one all by itself by generating > > > your JSON data and simply copying and pasting it > > > intowww.jsonlint.comtosee if it accepts it. That way you > > don't even > > > have to think about JavaScript at all; you are simply > > debugging yourRailsapp. > > > > Once you know you have good JSON data, you can test > > question #2 with a > > > simple test case that just downloads the JSON data with jQuery and > > > displays it (even with just a console.log call). > > > > Finally, with all that working, you can tackle thetreeviewquestion. > > > > -Mike > > > > > From: Max Williams (Brighton) > > > > > Hi - first of all this is a plugin-specific question > > (abouttreeview) > > > > - i sent it to the plugin discussion page but it seems > > pretty dead > > > > (no posts for over a year), so i'm sending it here as well. If > > > > anyone could help me out that would be fantastic. > > > > > I've been usingtreeviewand have no problems with it so far. > > > > However, to get better performance i'm now trying to > > switch to the > > > >asynchronous version: > > > >http://jquery.bassistance.de/treeview/demo/async.html > > > > > In the example they use php to return some json to the > > tree, but i'm > > > > using it in a ruby onrailsapp, and can;t work out how to > > get it to > > > > work. Can anyone help? I'm really just not sure how to get the > > > > required json for the update back to thetreeview. > > > > > This is what i'm doing at the moment: > > > > > In the view: > > > > > jQuery(document).ready(function(){ > > > > jQuery("#prop-tree").treeview({ > > > > url: "tree/self_and_children" > > > > }); > > > > }); > > > > > ... > > > > <ul id="prop-tree"> > > > > </ul> > > > > > The url "tree/self_and_children" does seem to be calling > > the correct > > > > controller and action, which is as follows: > > > > > def self_and_children > > > > # expects the id of the branch which is clicked on, > > which will > > > > be something like > > > > # "property_id_79". We want property with id 79. > > > > if params[:id] > > > > property = Property.find(params[:id].split("_").last) > > > > else > > > > property = Property.root > > > > end > > > > > @json = property.self_and_children_to_json > > > > respond_to do |format| > > > > if @json > > > > #should never get an html request for this > > > > format.html { render :text => @json } > > > > format.xml { head :ok } > > > > format.js { render :text => @json } > > > > else > > > > format.html { } > > > > format.xml { render :xml => @json.errors, :status => > > > > :unprocessable_entity } > > > > format.js > > > > end > > > > end > > > > end > > > > > But, nothing comes back - at least, the tree doesn't change. > > > > My questions are as follows: > > > > > a) is doing "render :text => @json" the proper way to > > send back the > > > > chunk of json totreeview? Should i do something in a js.rjs file > > > > instead? > > > > b) how do i send through the id of the clicked-on branch to the > > > > controller? (and retrieve it in the controller) > > > > > thanks in advance > > > > max