我正在用 Ruby on Rails 创建一个网站,用户可以使用 RESTful 身份验证登录。有人可以使用 html、xml 和 json 获取特定用户,就像脚手架一样。但我想再添加一种格式:vCard(例如/users/1.vcard)。这有一个特定的格式,但我如何定义自己的格式?使用 View ,还是必须使用其他方式?谢谢
最佳答案
在您的 /config/initializers/mime_types.rb 文件中,为您的格式添加新的注册。它应该看起来像这样:
Mime::Type.register "text/x-vcard", :vcard #The :vcard is the important part
之后(您必须重新启动应用才能获得更改),您可以像任何其他格式一样响应符号:
# then in your controller action
def show
respond_to do |format|
format.html # render html
format.vcard { #render vcard }
end
end
从评论中添加(感谢 nanda):
然后,在您的 View 文件夹中,您可以将 vCard 模板放入 show.vcard.erb 文件中(例如)。
https://stackoverflow.com/questions/2438868/