James' Blog

James’ personal blog

You are currently browsing the Ruby on Rails category.

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, 7 hours ago at 10:26 am.

Add a comment

Using JQuery + Prototype in Rails

Original info was found here.  It was posted about 3 years ago, so JQuery has changed a little since then with the implementation of JQuery.noconflict().

In a nutshell, just in clude JQuery first and then include the other libraries(s) that may use the $( function. Make sure you call JQuery stuff with JQuery(xxx) instead of $( from now on.  You include JQuery in rails usually in your main layout file by typing:

<%= javascript_include_tag ‘jquery’ %>

Make sure the jquery download files are in your /public/javascript directory of your project.

The JQuery site suggested adding the noconflict at the end of the jquery.js file, but instead I did it this way in the layout file.

<script>jQuery.noConflict(); </script>

Posted 9 months ago at 2:10 pm.

Add a comment

change validation error names in rails

Put the below code in to your model.  I found this info on railsforums.com.

HUMANIZED_COLUMNS = {
:Column_name => “New Name”,
:OtherColumn_name => “New Name 2″
}
def self.human_attribute_name(attribute)
HUMANIZED_COLUMNS[attribute.to_sym] || super
end

Posted 9 months ago at 2:01 pm.

Add a comment

Accessing domain name but pointing it to your local machine

There’s a simple trick I used to point a www.SomebodysDomain.com to my local machine since I’m doing development. He had hardcoded his domain name into his application which causes problems for when you don’t want to test on the production server.

I just edited my hosts file and added the line in /etc/hosts (OSX)

127.0.0.1 www.SomebodysDomain.com

Posted 1 year, 4 months ago at 12:18 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

Hosed my SSH

Hosed ssh on my slicehost.com

1. installed openssl when it was ALREADY THERE

2. accidentally deleted libcrypt.a (on hardy) in an attempt to delete libcrypto.a

3. the make install installed openssl to /usr/local/lib and now ssh was looking there first instead of /usr/lib. It threw some error about not recognizing the version and ssh wouldn’t start

Great help from slicehost. They even sent me their own copy of libcrypt.a. I ended up doing a few sudo apt-get remove openssh-server. Don’t know if that did anything.

/etc/init.d/ssh start (or reload) with no problems after removing the files from /usr/local/lib

I’ll use apt-get from now on.

I think i understand how the openssl libs work now. If you do a `locate libcrypto.so` it shows you that it’s in /usr/lib. I think that whatever you put in /usr/local/lib overrides what’s in /usr/lib. For some reason the tclink build.sh was looking only in /usr/local/lib and so i put a symlink to the /usr/lib instead of trying to download and compile my own openssl libs.

Then I copied the tclink.so library in to /usr/lib/ruby/1.6/x86_64-linux so all the Ruby can use it.

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

Authorize.net workings

So today I ran through our first transaction after getting some good advice from an integration specialist named Rob from Authorize.net.

At first he only gave us a test api login and transaction key to perform test transactions. With this account I wasn’t able to log into the the website and look at all the transactions and see that the details had been captured correctly. After asking for the ability to login and check transaction details, I got another test account from authorize.net.

With this account I had to:
1. go to “Account” and then create a new API login / Transaction Key
2. TURN OFF TEST MODE (not turning this off will not allow you to capture transactions)
3. Do another test transaction
4. win!

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

Add a comment