我在尝试运行我编写的代码时遇到错误。它是制作xml文件的代码,它从数据库中读取以获取所有信息。错误是由两行,Document.new和Element.new
引起的如果我注释掉这两行,那么代码就可以了。我不确定为什么这样做,因为那些类应该工作正常。
我的代码如下:
def create_google_file
@products = Product.find(:all)
file = File.new('dir.xml','w')
doc = Document.new
root = Element.new "rss"
root.add_attribute("xmlns:g", "http://base.google.com/ns/1.0")
root.add_attribute("version", "2.0")
channel = Element.new "channel"
root.add_element channel
title = Element.new "title"
title.text = "Sample Google Base"
channel.add_element title
link = Element.new "link"
link.text = "http://base.google.com/base/"
channel.add_element link
description = Element.new "description"
description.text = "Information about products"
channel.add_element description
@products.each do |y|
item = channel.add_element("item")
id = item.add_element("g:id")
id.text = y.id
title = item.add_element("title")
title.text = y.title
description = item.add_element("description")
description.text = y.description
googlecategory = item.add_element("g:google_product_category")
googlecategory.text = y.googlecategory
producttype = item.add_element("g:product_type")
producttype.text = y.producttype
link = item.add_element("link")
link.text = y.link
imglink = item.add_element("g:image_link")
imglink.text = y.imglink
condition = item.add_element("condition")
condition.text = y.condition
availability = item.add_element("g:availability")
availability.text = y.availability
price = item.add_element("g:price")
price.text = y.price "USD"
gtin = item.add_element("g:gtin")
gtin.text = y.gtin
brand = item.add_element("g:brand")
brand.text = y.brand
mpn = item.add_element("g:mpn")
mpn.text = y.mpn
expirationdate = item.add_element("g:expiration_date")
expirationdate.text = y.salepricedate
end
doc.add_element root
file.puts doc
file.close
end
我得到的错误是: ProductsController#create_google_file
中的NameError未初始化的常量ProductsController :: Element
和
ProductsController#create_google_file
中的NameError未初始化的常量ProductsController :: Document