gpt4 book ai didi

ruby-on-rails - 如何在 ruby​​ on rails 中渲染 .builder 模板?

转载 作者:数据小太阳 更新时间:2023-10-29 02:18:19 26 4
gpt4 key购买 nike

我真的很困惑如何在 ruby​​ on rails 中使用构建器模板。我有一些简单的 Controller 代码:

class ProductsController < ApplicationController

def index
@products = Product.all
respond_to do |format|
format.html # index.html.erb
format.xml # index.builder
end
end

end

但这似乎行不通。我的 index.builder 文件如下所示:

xm = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
xm.instruct!
xm.index{
@index.each do |i|
xm.country(i.country.name)
xm.value(i.value)
xm.year(i.year)
end
}

但我一直收到空白回复。看来我不理解这里的一些基本知识。

最佳答案

index.builder 重命名为 index.xml.builder 然后 xml 对象已经在 index.builder 中可用,所以你可以调整你的构建器文件看起来像这个:

xml.instruct!
xml.posts do
@products.each do |product|
xml.product do
xml.title product.title
xml.body product.body
xml.price product.price
end
end
end

更多信息:http://danengle.us/2009/05/generating-custom-xml-for-your-rails-app/

关于ruby-on-rails - 如何在 ruby​​ on rails 中渲染 .builder 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2309011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com