【问题标题】:sitemap_generator gem creates UrlGenerationErrorsitemap_generator gem 创建 UrlGenerationError
【发布时间】:2017-01-14 22:07:00
【问题描述】:

我使用(其中包括):

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

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

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

现在,当我 rake sitemap:refresh:no_ping

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

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

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

最后我的 routes.rb 是:

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

【问题讨论】:

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


    【解决方案1】:

    问题是我必须在我的 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
    

    【讨论】:

      【解决方案2】:

      生成站点地图真的很简单这里是你需要知道的事情

      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
      

      创建站点地图没有其他意义

      我希望这会有所帮助:)

      【讨论】:

      • 谢谢你,我会试试...看起来很简单:-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 2019-12-18
      相关资源
      最近更新 更多