Ruby on rails 控制器中的名称错误

Ruby on rails 控制器中的名称错误,ruby-on-rails,ruby,xml,Ruby On Rails,Ruby,Xml,尝试运行我编写的代码时出错。这是生成xml文件的代码,它从数据库中读取以获取所有信息。错误是由两行造成的,Document.new和Element.new 如果我注释掉这两行代码,那么代码就可以正常工作。我不知道为什么要这样做,因为这些课程应该运行得很好 我的代码如下: def create_google_file @products = Product.find(:all) file = File.new('dir.xml','w') doc = Document.new

尝试运行我编写的代码时出错。这是生成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_文件中的名称错误

未初始化的常量ProductsController::元素

ProductsController#create_google_文件中的名称错误


未初始化的常量ProductsController::Document

定义文档和元素的位置?你确定rails会用这些类加载文件吗?我相信是的,因为我在上找到了一个教程,它似乎说rails随这些类一起加载得很好,但在该教程中,它们使用不同的命名
REXML::Document
,而你只需键入
Document
。此外,在使用之前,您应该需要此库,是的,它位于默认库中,但默认情况下不加载,请在控制器文件
require“rexml/document”的顶部写入
我更改了它并添加了文档和元素的require部分,但现在我收到一条错误消息,说它无法加载这样的文件--element您不应该单独要求元素,它是rexml/document所必需的。你可能应该阅读官方文件