Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:17:58+00:00 2026-06-13T13:17:58+00:00

I’m having an unusual problem in an application I am developing. I am writing

  • 0

I’m having an unusual problem in an application I am developing.

I am writing a controller/view to generate a sitemap.xml resource. It works perfectly the first time I test it by viewing it in the browser.

However the second time, when I refresh the view, no page results are returned and the sitemap is effectively empty.

If I make a change to the code. It could be as little as adding a blank line, or removing it, then the sitemap is correctly generated the first time. Any additional refreshes are empty.

This is the code:

The sitemap controller

class SitemapController < ApplicationController
    layout nil

    def index
        @pages = Page.where(:publish => true)
        @base_url = "http://#{request.host_with_port}"
        headers['Content-Type'] = 'application/xml'
        def index
            respond_to do |format|
                format.xml
            end
        end
    end
end

Here is sitemap.xml.erb

<?xml version="1.0" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<% @pages.each do |page| %>
  <url>
    <loc><%= "#{@base_url}/#{page.permalink}" %></loc>
    <lastmod><%= page.updated_at %></lastmod>
    <priority>0.5</priority>
  </url>
<% end unless @pages.nil? %>
</urlset>

and the route

match "/sitemap.xml", :to => "sitemap#index", :defaults => {:format => :xml}

The odd thing is that it seems to be in the query in the controller.

@pages = Page.where(:publish => true)

This returns nil on consecutive attempts, but a similar query in other parts of the app works every time. I have tried using alternative methods such as Page.all, and Page.find :all but the problem persists.

I also uploaded this to the app on Heroku, wondering if it was something in my environment, but it happens there also.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:17:59+00:00Added an answer on June 13, 2026 at 1:17 pm

    Your SitemapController#index method is redefining itself. To clarify the problem:

    def fn
      def fn
        2
      end
      1
    end
    
    fn
    # => 1
    fn
    # => 2
    

    Try instead:

    class SitemapController < ApplicationController
        layout nil
    
        def index
            @pages = Page.where(:publish => true)
            @base_url = "http://#{request.host_with_port}"
            headers['Content-Type'] = 'application/xml'
            respond_to do |format|
                format.xml
            end
        end
    end
    

    Also, the sitemap_generator gem works rather well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I've tracked down a weird MySQL problem to the two different ways I was
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am currently running into a problem where an element is coming back from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.