sitemap_generator gem创建UrlGenerationError

时间:2017-01-14 22:07:00

标签: ruby-on-rails ruby-on-rails-4 sitemap sitemap-generator-gem

我使用(其中包括):

class Trip < ApplicationRecord
  has_many :preloaded_stop_times, 
           -> { where("stop_times.schedule_id = trips.schedule_id") },               
           class_name: "StopTime", 
           primary_key: :guid, 
           foreign_key: :trip_guid
end

# Usage
trips = Trip.joins(:preloaded_stop_times).where(...)
# ...

# with :eager_load
trips = Trip.eager_load(:preloaded_stop_times)

trips.each do |trip|
  stop_times = trip.preloaded_stop_times
  # ...
end

Sitemap生成器应为我的所有图片创建网址:

gem 'rails', '4.0.0'
gem 'sitemap_generator', '3.4'
gem "friendly_id", "~> 5.0.3"
gem 'globalize', '~> 4.0.2'

我的站点地图生成器通常运行良好,但不适用于图像模型。相关代码是:

class Image < ActiveRecord::Base
  attr_accessible :description, :name, :size, :image, 
                  :tag_ids, etc...

  has_many :taggings, :dependent => :destroy
  has_many :tags, :through => :taggings
  has_and_belongs_to_many :articles
  mount_uploader :image, ImageUploader
  extend FriendlyId
  friendly_id :name, use: [:slugged, :history]
  translates :name, :description
end

现在,当我做rake sitemap时:刷新:no_ping

ActionController :: UrlGenerationError:没有路由匹配{:action =&gt;&#34; show&#34;,:controller =&gt;&#34; images&#34;,:locale =&gt;#,:id = &gt; nil,:format =&gt; nil}缺少必需的键:[:id]

我认为您可能需要更多信息来帮助我,但我不知道是什么。该网站运行良好的两种语言和rake:routes给出:

[nil, :de].each do |locale|
  Image.find_each do |image|
    sitemap.add image_path(image), :changefreq => 'monthly'
  end
end

最后我的routes.rb是:

images GET (/:locale)/images(.:format)       images#index {:locale=>/en|de/}
POST (/:locale)/images(.:format)             images#create {:locale=>/en|de/}
new_image GET  (/:locale)/images/new(.:format)  images#new {:locale=>/en|de/}
edit_image GET (/:locale)/images/:id/edit(.:format) images#edit {:locale=>/en|de/}
image GET (/:locale)/images/:id(.:format)    images#show {:locale=>/en|de/}
PATCH  (/:locale)/images/:id(.:format) images#update {:locale=>/en|de/}
PUT (/:locale)/images/:id(.:format) images#update {:locale=>/en|de/}
DELETE (/:locale)/images/:id(.:format) images#destroy {:locale=>/en|de/}

2 个答案:

答案 0 :(得分:0)

问题是我必须在sitemap.rb中传递区域设置。所以sitemap.rb中的正确代码是:

image = Image.all

[nil, :de].each do |locale|
  image.find_each do |image|
    sitemap.add image_path(image, :locale => locale)
  end
end

答案 1 :(得分:0)

生成SiteMap非常简单以下是您需要了解的内容

1)路线

  #config/routes.rb
  get 'sitemap.xml', :to => 'sitemap#index', :defaults => {:format => 'xml'}
  root '...'

2)控制器

#app/controllers/sitemap_controller.rb
class SitemapController < ApplicationController
  layout nil
  def index
    headers['Content-Type'] = 'application/xml'
    respond_to do |format|
      format.xml {
        @images = Image.all
      }
    end
  end
end

3)现在我使用了haml

#app/views/sitemap/index.xml.haml
!!! XML
%urlset{:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9"}
  - @images.each do |image|
    %url
      %loc #{image_url(image)}
      %lastmod=image.updated_at.strftime('%Y-%m-%d')
      %changefreq weekly
      %priority 0.5

创建站点地图没有别的了解

我希望这会有所帮助:)