2012-10-31 5 views
1

개발중인 응용 프로그램에서 비정상적인 문제가 있습니다.레일 쿼리 결과 처음으로 만 반환

sitemap.xml 리소스를 생성하기 위해 컨트롤러/뷰를 작성했습니다. 브라우저에서 처음으로 테스트 할 때 완벽하게 작동합니다.

그러나 두 번째로보기를 새로 고칠 때 페이지 결과가 반환되지 않고 사이트 맵이 실제로 비어 있습니다.

코드를 변경하면 어떻게됩니까? 빈 줄을 추가하거나 제거하는 것만 큼 작은 경우 처음으로 사이트 맵이 올바르게 생성됩니다. 추가 새로 고침은 비어 있습니다.

사이트 맵 제어기 여기

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 

가 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> 

및 경로

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

홀수 일 :

는 코드 그거야? 컨트롤러의 쿼리에 있어야합니다.

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

이렇게하면 연속 시도에서 nil을 반환하지만 앱의 다른 부분에서 비슷한 쿼리가 매번 작동합니다. 나는 Page.all, Page.find와 같은 대안적인 방법을 사용해 보았습니다 : 문제는 모두 지속되었지만 계속되었습니다.

또한 이것을 Heroku의 앱에 업로드하여 내 환경에 어떤 것이 있는지 궁금해하지만 실제로도 발생합니다.

답변

3

SitemapController#index 방법 자체가 다시 정의됩니다. 문제를 명확히하기 위해 :

def fn 
    def fn 
    2 
    end 
    1 
end 

fn 
# => 1 
fn 
# => 2 

대신보십시오 : 또한

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 

sitemap_generator gem 작품 오히려 잘.

+0

감사합니다. –