Rails:响应XML

时间:2009-08-22 19:11:12

标签: ruby-on-rails

我正在寻求回应XML。在我的演出动作中,我有这样的事情:

respond_to do |format|
  format.html {
    render :action => 'show'
  }
  format.xml {
    render :xml => @post.to_xml
  }
end

这将输出所有帖子的属性。你将如何仅输出一些帖子的属性。另外,说Post belongs_to User。您如何通过使用帖子的XML(而不是给帖子的外键)输出用户名来更进一步?

2 个答案:

答案 0 :(得分:4)

@post.to_xml(:except => [:foo, :bar], :include => :user)

to_xml上的文档详细介绍

答案 1 :(得分:1)

事实证明,您可以将:only和:except选项传递给:include:

@post.to_xml(:only => [:created_at, :updated_at], :include => {:user => {:only => :name}})

这将获得帖子的created_at和updated_at列以及相关用户的名称。