Tuesday, July 8, 2008

Google ad Sense code implementation with Ruby on Rails

I needed to integrate Google Adsense with my Ruby On Rails application. It was quiet easy actually but initally it was somewhat time consming to search some bits and pieces.

So I am putting all the code here so that it may help you and I hope you will have fun with it.

//////// *************** 4 Steps *********//////////////

step 1 - Make a controller named ads (ruby script\generate controller ads) ,ads_controller.rb file, and replace code with the ocntent of the file at

Download AdsController.rb

Step 2 - In your rails hierarchy goto app/helpers/application_helper.rb put following code

def google_ad
content_tag("iframe",nil , :scrolling => "no", :frameborder => 0 ,
:width => 800, :height => 80,
:src => url_for(:controller => "ads", :action => "adsense",
:canvas => false, :only_path => false))
end
end

Step 3 - Go to app/views and in the rhtml file corresponding to the view add javacript code to call the function name "google_ad()"

one line code for this simple call is similar to



You can place it where ever you want to display adds.

Step4 - Refresh Code and GAdsense should be working :) You can play with the formating in ads_controller.rb file

2 comments:

Unknown said...

Cannot download controller!!!!

Anonymous said...

me also, can't download controller.rb file.