Follow Along as I Stumble on the Path to Learning RoR

heroku

Adding Heroku Postgresql to PGAdmin

To add your Heroku DB to PGAdmin, first get your credentials from Heroku.

You’ll need your:

  • host
  • DB name
  • port
  • user name
  • password

Add a new server using these credentials.  In addition, you’ll want to change the server settings, or you’ll see about a thousand DB names in your list.

  1. make sure you are disconnected from the server,
  2. select the Server, right click -> properties, Advanced tab,
  3. in ‘DB restriction’ type in the name of your database(s) enclosed in single quotes and separated by spaces (i.e. ‘db_name’).

Now you’re server should be able to connect and you should just see your DB in the “Databases” section under your server.


Adding a Sitemap to Google for Sites on Heroku

I’m using the awesome Sitemap Generator gem for Rails  and also hosting my app on Heroku.  Since I’m updating my sitemap daily, and Heroku forces me to write to somewhere else, I need to create the sitemap nightly on Amazon Web Services (S3), instead.

The problem I ran into is that when I went to Google Webmaster Tools, it only allowed me to specify a url for the sitemap beginning with the name of my domain.  However, since the sitemap is hosted on AWS, it has an address that starts with an Amazon URL.

I found an answer here.

Basically what you need to do is to add a second property in Google Webmaster Tools for your S3 bucket.  I added a property to my directory with the sitemap (i.e. http://s3.amazonaws.com/mybucket/sitemaps/) and then verified that I owned the property by placing the Google supplied HTML file in the sitemaps folder on S3.

The last step is adding the URL to the sitemap in settings for the new [S3] property that was added to Google Webmaster Tools.


Creating a Sitemap Automagically for Rails App

Sitemaps help in making sure that your app is properly indexed, so it would help to automatically have them generated (and updated) so a search engine knows when a new web page is created, or an old one updated.  Luckily, there’s a gem, SitemapGenerator that makes it extremely easy to do this.

All you have to do  in Rails 3 is:

  1. add: gem ‘sitemap_generator’ to your gemfile
  2. run “rake sitemap:install” which creates config/sitemap.rb
  3. customize sitemap.rb

A simple example (where “Content” is a model name)

SitemapGenerator::Sitemap.default_host = "http://www.example.com"
SitemapGenerator::Sitemap.create do
  add '/contact_us'
  Content.find_each do |content|
    add content_path(content), :lastmod => content.updated_at
  end
end

4. run “rake sitemap:refresh”

5.  change your robots.txt file (if you have one) to include this line (so search engines can find it easy):

Sitemap: http://www.example.com/sitemap_index.xml.gz

6.  if you’re using Heroku, like me, you’ll have to take extra steps, as in this example, to write the files to somewhere else since Heroku is read-only.

7.  Also, if you’re using Heroku, you may want to use Heroku Scheduler to automatically refresh the sitemap by adding this task:

$ rake sitemap:refresh

8.  Lastly, to make sure Google indexes your site better, you’ll want to enter the path to your sitemap in a Google Webmaster Tools account for your site – the same as the address in step 5, unless you are using Heroku or another read-only system (and the sitemap is stored on another domain).  In this case, since Google Webmaster tools only accepts url’s from your domain (for the sitemap location), you’ll have to re-route them to the external location.  To do that you can make a simple controller like this example from Bill Rowell or you can use the rack-rewite gem.


Getting ActiveAdmin to Work on Heroku

The ActiveAdmin gem was working for me in development, but not in production.  The reason, I found, was because not all of the assets used by ActiveAdmin were getting precompiled by Heroku.  To solve the issue, I used the code by Ryan Long’s answer on Stack Overflow (in production.rb).

# Precompile additional assets. Defaults to [application.js, application.css, non-JS/CSS]
config.assets.precompile += ['active_admin.css', 'active_admin/print.css', 'active_admin.js']

Configuring my Staging Site on Heroku

I set-up a staging site so I could do QA before pushing to production.  In order to do so, I created a new Heroku app, and had to set-up new API’s.  I was wondering about the best way to make sure that both the staging and production app had the correct API credentials without overwriting the YML’s.

I found this article on Heroku that shows exactly how to handle such a situation.  Store your credentials on Heroku and reference them from the code.

https://devcenter.heroku.com/articles/config-vars


How to Find How Much Memcache Memory You’re Using

I wanted to figure out how much Memcache memory I was using for my Heroku Rails app.  After all, how would I know if I need to upgrade otherwise, unless I keep checking the logs for the word ‘miss’.

I found out through this post on Stackoverflow.

From the console (‘heroku run console’ with the Cedar stack), enter

Rails.cache.stats

it will provide a bunch of stats, and towards the end you will see bytes.