如何在rails上的ruby中呈现.builder模板?
我真的很困惑如何在rails上的ruby中使用构建器模板。 我有一些简单的控制器代码:
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
,然后在index.builder中已经可以使用xml对象,因此您可以调整构建器文件,如下所示:
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/
- open_id_authentication – “OpenIdAuthentication.store为零。 使用内存存储。“问题
- 我应该如何在ApplicationController中使用Draper?
- Rails Bootstrap如何格式化form_for(宽度网格折叠)
- 为什么Ruby on Rails secretts.yml没有加载?
- 以简单forms向文本输入添加默认值
- Rails在渲染后强制重新加载javascript
- Rspec spec和Rake规范给出了不同的结果
- Sencha Touch Rails 3.1
- 使用http基本身份validation和restful_authentication插件注销