I’m writing a Rails app & at the moment I’m making sitemap generator. Everything was just fine, and is still fine on my computer. But when I deploy it to Heroku I get a problem.
The generator works pretty simple: it just writes all urls of one resource and the code looks like this:
Controller:
class SitemapController < ApplicationController
layout nil
def index
@solutions = Solution.find(:all, :order => "updated_at DESC")
@base_url = "http://#{request.host_with_port}"
headers['Content-Type'] = 'application/xml'
def index
respond_to do |format|
format.html
format.xml
end
end
end
end
View:
<% if @solutions %>
<url>
<loc><%= "#{@base_url}#{solutions_path}" %></loc>
<lastmod><%= @solutions.first.updated_at.to_s(:sitemap) %></lastmod>
<priority>0.6</priority>
</url>
<% @solutions.each do |solution| %>
<url>
<loc><%= "#{@base_url}#{url_for(solution)}" %></loc>
<lastmod><%= solution.updated_at.to_s(:sitemap) %></lastmod>
<priority>0.5</priority>
</url>
<% end %>
As I’ve already told, locally it works all fine, but on Heroku the expected result is shown just once after deployment, all other times no links are shown as no solutions are selected.
Here are the logs:
2012-01-05T16:32:34+00:00 app[web.1]: Started GET "/sitemap.xml" for 194.44.214.138 at 2012-01-05 16:32:34 +0000
2012-01-05T16:32:34+00:00 app[web.1]:
2012-01-05T16:32:34+00:00 app[web.1]: Processing by SitemapController#index as XML
2012-01-05T16:32:34+00:00 app[web.1]: Solution Load (5.1ms) SELECT "solutions".* FROM "solutions" ORDER BY updated_at DESC
2012-01-05T16:32:34+00:00 app[web.1]: (1.4ms) SHOW search_path
2012-01-05T16:32:34+00:00 app[web.1]: Rendered sitemap/index.xml.erb (19.4ms)
2012-01-05T16:32:34+00:00 app[web.1]: Completed 200 OK in 92ms (Views: 70.4ms | ActiveRecord: 21.3ms)
2012-01-05T16:32:34+00:00 app[web.1]: cache: [GET /sitemap.xml] miss
2012-01-05T16:32:40+00:00 app[web.1]:
2012-01-05T16:32:40+00:00 app[web.1]:
2012-01-05T16:32:40+00:00 app[web.1]: Started GET "/sitemap.xml" for 194.44.214.138 at 2012-01-05 16:32:40 +0000
2012-01-05T16:32:40+00:00 app[web.1]: Processing by SitemapController#index as XML
2012-01-05T16:32:40+00:00 app[web.1]: Rendered sitemap/index.xml.erb (0.0ms)
2012-01-05T16:32:40+00:00 app[web.1]: Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)
2012-01-05T16:32:40+00:00 app[web.1]: cache: [GET /sitemap.xml] miss
2012-01-05T16:32:40+00:00 heroku[router]: GET www.my.url/sitemap.xml dyno=web.1 queue=0 wait=0ms service=9ms status=200 bytes=299
2012-01-05T16:32:43+00:00 app[web.1]:
2012-01-05T16:32:43+00:00 app[web.1]:
2012-01-05T16:32:43+00:00 app[web.1]: Started GET "/sitemap.xml" for 194.44.214.138 at 2012-01-05 16:32:43 +0000
2012-01-05T16:32:43+00:00 app[web.1]: Processing by SitemapController#index as XML
2012-01-05T16:32:43+00:00 app[web.1]: Rendered sitemap/index.xml.erb (0.0ms)
2012-01-05T16:32:43+00:00 app[web.1]: Completed 200 OK in 0ms (Views: 0.3ms | ActiveRecord: 0.0ms)
2012-01-05T16:32:43+00:00 app[web.1]: cache: [GET /sitemap.xml] miss
2012-01-05T16:32:43+00:00 heroku[router]: GET www.my.url/sitemap.xml dyno=web.1 queue=0 wait=0ms service=7ms status=200 bytes=299
2012-01-05T16:32:44+00:00 app[web.1]:
2012-01-05T16:32:44+00:00 app[web.1]:
2012-01-05T16:32:44+00:00 app[web.1]: Started GET "/sitemap.xml" for 194.44.214.138 at 2012-01-05 16:32:44 +0000
2012-01-05T16:32:44+00:00 app[web.1]: Processing by SitemapController#index as XML
2012-01-05T16:32:44+00:00 app[web.1]: Rendered sitemap/index.xml.erb (0.0ms)
2012-01-05T16:32:44+00:00 app[web.1]: Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)
2012-01-05T16:32:44+00:00 app[web.1]: cache: [GET /sitemap.xml] miss
2012-01-05T16:32:44+00:00 heroku[router]: GET www.my.url/sitemap.xml dyno=web.1 queue=0 wait=0ms service=30ms status=200 bytes=299
So as you can see the Solution Load is issued only on a first request and skipped during the other ones.
Do you have any idea what’s the problem?
By the way I haven’t tried out the previous version, but from the Google webmaster tools it seemed that everything was fine. Could this be caused by the formatting changes I’ve made? I’ve added the next line in
config/initializers/time_formats.rb
Time::DATE_FORMATS[:sitemap] = "%Y-%m-%d"
and in the controller
.to_s(:sitemap)
method calls to the updated_at.
I’ll be very thankful for any kind of reply or hint.
Your controller code is all kinds of fruity. For some reason you have
def indextwice (within itself). You should use: