James' Blog

James’ personal blog

return JSON to Rails via AJAX

If you need to request some data asynchronously via AJAX one option is to use the Ajax.request object from prototype.  If you call/create this object you can create a callback function for when the function completes (onComplete) and this will have the return data in it ready for you to process.

In my case I used my Rails controller to return JSON data first on the backend:

result_lat_lon = processNgaNums(params[:map])

respond_to do |format|
    format.json { render :json => result_lat_lon.to_json}
end

Then on the client side I created an ajax request:

new Ajax.Request(path, {
         asynchronous:true,
         evalScripts:true,
         method:'get',
         onComplete: function(transport){
                       var json = transport.responseText.evalJSON();
                       jQuery.each(json, function(i, val){
                          locations.push([i, val[0], val[1]]);
                       });
                       setMarkers(map, locations);
                     }
        });

The main block of code to take note of here is to declare the transport variable which the onComplete function will receive when it is called.  Then you can do an evalJSON on that text and treat it as an array.   Also in the loop to process the JSON each entry contains a key and a value which corresponds to the i and val variables.

Posted 6 days, 6 hours ago at 10:26 am.

Add a comment

Netbeans debug Ruby

Netbeans 6.0.1
Problem occurred during debugger start: Cannot connect to the debugged process in 10s.

From terminal:

gem install ruby-debug

gem install ruby-debug-base

gem install ruby-debug-ide

didn’t do anything. I did the same thing from within Netbeans and it worked. I think it’s looking at it’s own set of gems. Then it asked me to install Fastdebug, but it failed. At least I can debug now again.

Posted 2 years, 3 months ago at 11:37 am.

Add a comment

Netbeans for Ruby on Rails?

Textmate is great. It really is.

anyway, Netbeans

Netbeans gives you the full power of an IDE over a standard text editor.  With code completion, in code debugging and the ability to see all of your local variables visually.
Tried to install the debug gem directly, don’t want to make my “own” copy of gems of Ruby. I want to use my current copy which is just fine.

Make myself the owner of the systems gems (not my local gems) which was in /Library

*update*
I guess netbeans didn’t know where to look for the gems.
thanks to G. Hamilton here for finding a fix for this. I guess 6.1 is supposed to fix the leopard problems. the solution was to put this:

export GEM_PATH=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8:/Library/Ruby/Gems/1.8
export GEM_HOME=/Library/Ruby/Gems/1.8

in here:

$HOME/.netbeans/6.0/etc/netbeans.conf

*side note*
Apparently changing the permissions on on the /usr/bin directory isn’t a good idea. Who would have thought? I thought I would be able to change it back because I live in the magical land of make believe along with barbie. Now all my sudo and su and other permissions are screwed up. I guess in ubuntu you can start the recovery console, instead…
popped in the OSX install disk and booted
ran disk utility
hit the fix permissions button, done!

Posted 2 years, 3 months ago at 11:37 am.

Add a comment

Ruby on Rails E-commerce book

On page 148 of this title book it shows you how to use inject a most useful function.

I wanted to have some conditionals so i just decided to use the for loop instead, following blindly, I typed this:

sum =+ item.price * item.amount

Obviously, can see copying this piece of could would not work since =+ != +=.  Bang head on wall.

Posted 2 years, 3 months ago at 11:37 am.

Add a comment

Rails Migrations

Reached a new level of stupidity today.

rake db:migrate version=25

it didn’t work…scratch head…3 episodes of Law and Order

rake db:migrate VERSION=25

fixed!

Posted 2 years, 3 months ago at 11:37 am.

Add a comment

c.error

Peepcode  has shown me that that in the ./script/console or where ever
if you do a c.save and get a false you can do a c.errors to see why!

Also RailCasts other useful tricks for script/console…

script/console –sandbox
-will roll back db changes so you don’t screw stuff up in console

y variable
-similar to p variable but prints it in yml, pretty

helper.field_for….
-helper represents the actionview so you can test your view codings

Posted 2 years, 3 months ago at 11:37 am.

Add a comment