如何将 lastmod 添加到我的应用程序的站点地图

How to add lastmod to my application's sitemap

我想将 lastmod 添加到我的站点地图,但我找不到一个很好的例子来说明应该如何设置它的格式。

这是我目前正在使用的东西:

控制器:

class SitemapsController < ApplicationController
  def show
    @events = Event.where(block_search_engines: false, status: 1)
  end
end

显示生成器:

  xml.instruct!
  xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do

  @events.each do |event|
    xml.url do
      xml.loc present_url(event.slug)
      xml.priority 1.0
    end
  end
end

我将如何在此代码中实现它?

根据sitemap.xml protocol specification, the value should be formatted in the W3C Datetime format,基本上就是ISO 8601。

The date of last modification of the file. This date should be in W3C Datetime format. This format allows you to omit the time portion, if desired, and use YYYY-MM-DD.

ruby 日期时间包含 methods 以获得 ISO 8601 格式的输出。

所以你可以做类似 xml.lastmod event.updated_at.xmlschema 的事情。