I have started using xml builder templates for most of my model. I need to find a generic way to build XML outside of a render which uses the .xml.builder template instead of the generic .to_xml method provided in the model
我已经开始在我的大部分模型中使用xml构建器模板。我需要找到一种通用的方法,在使用. XML的呈现之外构建XML。构建器模板,而不是模型中提供的通用的.to_xml方法
I gather that I will have to override the default to_xml (or add a to_my_xml), but I am unable to see how to get XMLBuilder to use my .builder files.
我认为我必须重写默认的to_xml(或者添加一个to_my_xml),但是我无法看到如何让XMLBuilder使用我的.builder文件。
Any ideas?
什么好主意吗?
3 个解决方案
#1
3
Add a respond_to
block in your controller so that the appropriate template is rendered according to the requested format. For example:
在控制器中添加respond_to块,以便根据请求的格式呈现适当的模板。例如:
def show
...
respond_to do |format|
format.html # renders show.html.erb
format.xml # renders show.xml.builder
end
end
#2
1
If you are looking for how to create xml using builder then this is how you can do it
如果您正在寻找如何使用builder创建xml,那么这就是您可以做到的
require 'rubygems'
require 'builder'
builder = Builder::XmlMarkup.new(:indent=>2)
builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
builder.my_elements do |e|
builder.myitem {|element| element.my_element_name('element_value')}
end
#=>
<?xml version="1.0" encoding="UTF-8"?>
<my_elements>
<myitem>
<my_element_name>element_value</my_element_name>
</myitem>
</my_elements>
#3
1
Hi I just had to solve this problem. Here is how I got it to work, comments inline ...
嗨,我刚刚解决了这个问题。这是我如何让它工作,评论内联……
class PostsController < ApplicationController
# this must be included to use tag helpers like cdata_block, etc ...
include :Helpers::TagHelper
def preview_xml
@post = Post.find(params[:id])
# You must set up a instance variable named 'xml'
xml = ::Builder::XmlMarkup.new(indent: 2)
builder_file = File.read(::Rails.root.to_s + '/app/views/posts/show.xml.builder')
require 'builder'
# instance_eval evaluates the file within the context of the current instance -- so the
# xml variable we just set will be accessable within the builder_file
instance_eval builder_file
@tag_xml = xml.target!
end
end
#1
3
Add a respond_to
block in your controller so that the appropriate template is rendered according to the requested format. For example:
在控制器中添加respond_to块,以便根据请求的格式呈现适当的模板。例如:
def show
...
respond_to do |format|
format.html # renders show.html.erb
format.xml # renders show.xml.builder
end
end
#2
1
If you are looking for how to create xml using builder then this is how you can do it
如果您正在寻找如何使用builder创建xml,那么这就是您可以做到的
require 'rubygems'
require 'builder'
builder = Builder::XmlMarkup.new(:indent=>2)
builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
builder.my_elements do |e|
builder.myitem {|element| element.my_element_name('element_value')}
end
#=>
<?xml version="1.0" encoding="UTF-8"?>
<my_elements>
<myitem>
<my_element_name>element_value</my_element_name>
</myitem>
</my_elements>
#3
1
Hi I just had to solve this problem. Here is how I got it to work, comments inline ...
嗨,我刚刚解决了这个问题。这是我如何让它工作,评论内联……
class PostsController < ApplicationController
# this must be included to use tag helpers like cdata_block, etc ...
include :Helpers::TagHelper
def preview_xml
@post = Post.find(params[:id])
# You must set up a instance variable named 'xml'
xml = ::Builder::XmlMarkup.new(indent: 2)
builder_file = File.read(::Rails.root.to_s + '/app/views/posts/show.xml.builder')
require 'builder'
# instance_eval evaluates the file within the context of the current instance -- so the
# xml variable we just set will be accessable within the builder_file
instance_eval builder_file
@tag_xml = xml.target!
end
end