Dec 12

I bought the Paypal Rails Integration ebook by Benjamin Curtis here.

But upon running the sample application, I cannot get through the purchase step and always get the error “There was an error processing your payment: Security header is not valid”

After some playing-around, although in the PDF it says “The modified version of the plugin included with the sample application does have the signature support.”, it turns out that the application doesnt have signature support.

So simply use PEM file instead of signature, like this:

ActiveMerchant::Billing::PaypalGateway.pem_file = path/to/file.pem

And remove the :signature element from PAYPAL_API_CREDENTIALS.

Dec 3

How do we cache an ajax response in Rails, so that we dont need to do an ajax the next time ?

It’s the question I’m facing on building an application that is AJAX-heavy, and it’s becoming important for the application to be high-performance.

After some googling, I came across several stuff. One is this interesting article on setting ‘Expires’ header in the HTTP response.

But well, I dont really know how to implement that in Rails.

Anyway his book, High Performance Websites seems like a very good book to buy!

So to cache an ajax response, I use the solution on this site.

Here is a simpler version of that:


<script>
var cache = new Array;
function myscript(somevalue) {
var url = "ajax/function?someparam=somevalue";
if (cache[url]) {
eval(cache[url]);
} else {
new Ajax.Request(url, {asynchronous:true, evalScripts:true,
onComplete:function(request) {
if(request.status == 200) { cache[url] = request.responseText; }
}});
}
</script>

Nov 29

It took me many hours to find out why my sessions are often gone when using memcache session storage.

Then I remember, that I cannot simply store any objects to a memcache session storage!

In the older rails, we have to do:

model :cart

to store an object of class Cart.

In the newer rails, just replace ‘model’ with ‘require_dependency‘.

For more info, read this site under If you have an “uninitialized constant” error

Nov 8

This happened to me today when using ActiveResource.

For example, Order.find(:all), whereby Order is an ActiveResource object, always returns the ‘cant mass-assign’ error.

The error is really not informative.

So after a couple of head-scratching hours, it turns out that if the result of Order.find(:all) includes a Shipment object, for example, then we should not have an ActiveRecord app/models/shipment.rb in our application.

It’s just a naming conflict it seems. I’m not really sure why this happens, but hopefully this helps others who may encounter the same problem.

Oct 14

Ever that kind of error when trying to use HttpMock to test your ActiveResource client code ?

I got that error, and for hours couldnt figure out why. I have followed the tests in the library (such as test/base_test.rb):

ActiveResource::HttpMock.respond_to do |mock|
mock.get “/people/1.xml”, {}, @somexml

Well, it turns out that I need to set .site = “http://localhost” on the ActiveResource class I’m using!

Hope this helps anyone who experience the same problem as mine.

But if you are developing both the web service client and the provider, it’s better to just execute the XML request to the provider application instead of doing HttpMock. Because well, it’s a local connection, so it’s fast. Also it can help find bugs in the provider application.

Sep 15

The order of which we load fixtures to our tables will need to be defined when we use foreign key constraints on our tables.

These would likely be the errors when doing rake db:fixtures:load

rake aborted!
Mysql::Error: #23000Cannot delete or update a parent row: a foreign key constraint fails

Basically it’s because rake db:fixtures:load loads fixtures (empties the tables and inserts back) alphabetically.

Here is the solution (which works great) from Mr. Olesen.. Basically just write the order in your environment.rb :

ENV[’FIXTURES’] ||= ‘accounts,developers,users’

Aug 29

How can our code take a screenshot of a webpage, and save it to an image file ?

It turns out it’s easy. First, we need to

gem install win32screenshot

Then, write code something like this:

require ‘win32screenshot’
width, height, bmp = Win32::Screenshot.foreground
img = Magick::Image.from_blog(bmp)[0]
img.write(RAILS_ROOT + “/public/images/screenshot.png”)

Voila, and you get the screenshot!

Aug 24

I was a PHP programmer before, I got used to ‘echo’ when trying to debug PHP application.
That’s why when migrating to Ruby from PHP, I used pp a lot. Although it just prints to console, it helps a lot.

But I’ve been wondering all this while, sometimes I got an application error, undefined method ‘pp’, while trying to pretty-print an object on Rails.

I never bothered to find out why, until today where I just happen to.

It turns out we have to require ‘PP’ on our code to make it works. Duh!

And make sure you use lowercase ‘pp’ if you are in a linux environment, otherwise it will say No such file to load

May 4

I was introduced to Trac about half a year ago, very nice and helpful!

Trac is not the only one project management tool out there of course, but I think it’s one of the best.

But setting it up (including the SVN integration) could be difficult. Especially on a shared hosting.

I came across this site, SVN Repository. It gives you unlimited Trac (and SVN) instances for only $6.95 a month. Very beneficial indeed! I can now give my clients who didnt have project management tool, a Trac each!

Apr 28

I’m in the United States now, being trained on a new application which one of my clients built.

Anyway, on my spare time at night I’ve found that Gmail can be used for your own domain!

And yes, it is FREE!

Finally we moved from crappy Horde webmail given by Bluehost to the best webmail ever, Gmail!

Not only that, Google gives us Calendar too. Each of our employees now use the Calendar to write daily work reports. Nice!

Next Entries »