Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “heroku

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']

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.


Getting Thin Gem to Work on Heroku When you Develop in Windows

Upgrading to Heroku’s Cedar stack, I had to install the Thin gem to prevent my app from running on Web Brick.  On the Bamboo stack this is injected so I need not worry, but with Cedar you need to install all the gems you need.

The problem is that I work on Windows in development and that one of the dependencies for the Thin gem is the Eventmachine gem.  However, Eventmachine isn’t really geared towards Windows environments (except gem install eventmachine –pre), so everytime went to run bundle install the install would fail because the dependency, eventmachine 0.12.10 could not install.

However, Heroku helped me find a solution.

Add this to the gemfile

group :production do
gem ‘thin’
end

and instead of ‘bundle install’, run ‘bundle install –without production’


Moving to 3.1 on Heroku

Took some lumps moving to 3.1 on Heroku.

Here’s some lessons:

  1. config.assets.initialize_on_precompile = false

    this has to be set per Rails Guides and Heroku.

  2. run “bundle exec rake assets:precompile” locally before pushing to Heroku.  I had a lot of stuff work locally, but when Heroku compiled it, it blew up.  There were a lot of things that I needed to change in my CSS files, working through them one by one
  3. the precompile generated files i uploaded (for the asset
  4. I had a bunch of views with javascript_include_tag and the asset pipeline wasn’t fond of that so I moved it into my application.cs and .js files to use instead
  5. I was getting some errors related to the Sass-rails gem even though it was included in my gemfile.  Turns out I had to move it so that it was outside of this block “group :assets do” that I had it in

Moving to 3.1 on Heroku

Took some lumps moving to 3.1 on Heroku.

Here’s some lessons:

  1. config.assets.initialize_on_precompile = false

    this has to be set per Rails Guides and Heroku.

  2. run “bundle exec rake assets:precompile” locally before pushing to Heroku.  I had a lot of stuff work locally, but when Heroku compiled it, it blew up.  There were a lot of things that I needed to change in my CSS files, working through them one by one
  3. the precompile generated files i uploaded (for the assets), as well as the manifest.yml file
  4. I had a bunch of views with javascript_include_tag and the asset pipeline wasn’t fond of that so I moved it into my application.cs and .js files to use instead