sitemap_generator gem creates UrlGenerationError

440 views Asked by At

I use (amongst others):

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

Sitemap generator should create urls to all my images:

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

My sitemap generator generally works well, but not for the Image model. The relevant code is:

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

Now, when I do rake sitemap:refresh:no_ping

ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"images", :locale=>#, :id=>nil, :format=>nil} missing required keys: [:id]

I think you might need more information to help here, but I have no idea what. The website runs well in two languages and rake:routes gives:

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/}

Finally my routes.rb is:

scope "(:locale)", locale: /en|de/ do
  resources :images do
    get 'confirm_destroy', :on => :member
  end
end
2

There are 2 answers

1
MZaragoza On BEST ANSWER

Generating a SiteMap is really simple Here are the things that you need to know

1) routes

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

2) controller

#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) the view now I used 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

There is nothing else to creating a site map

I hope that this helps :)

0
user929062 On

Problem was that I had to pass the locale in my sitemap.rb. So the correct code in sitemap.rb is:

image = Image.all

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