Creating XML With Rails 2.1

Posted by Tres Sun, 10 Aug 2008 18:08:00 GMT

So there’s lots of documentation out there on creating XML via Rails. The problem is that it’s not really kept up with all the changes that have occurred. Here’s a few quick items that may be helpful when creating XML with Rails 2.1.

• When creating an instance of XMLBuilder, use ::Builder::XmlMarkup.new instead of Builder::XmlMarkup.new.

• When creating your view, use the extension .xml.builder instead of .xml.erb

• When rendering the template from the controller, make sure that the content type headers are set for xml and that you’re not rendering the layout. The controller should have a section that looks something like this:


    headers["Content-Type"] = "text/xml"

    respond_to do |format|
      format.xml { render :template => 'options.xml.builder', :layout => false }
    end

Posted in , ,  | Tags ,

Comments are disabled