我有一个控制器动作
def index
@videos = Video.all.to_a
respond_to do |format|
format.xml { render :xml => @videos }
format.json { render :json => @videos }
end
end
视频具有属性
name
和title
。我希望返回xml仅包含
title
。如何从响应中限制它。
最佳答案
这样做:
def index
@videos = Video.all
respond_to do |format|
format.xml { render :xml => @videos.to_xml( :only => [:title] ) }
format.json { render :json => @videos.to_json( :only => [:title] ) }
end
end
您可以在the serialization documentation上找到有关此信息的更多信息。