开发者

Adding a Renderer for VCF in Rails 3

I'm attempting to add a renderer to allow one of my models to respond to the .vcf format. I have added the following code to my vcf_renderer.rb file which is in my 开发者_如何转开发initializers directory:

Mime::Type.register 'text/x-vcard', :vcf
ActionController::Renderers.add :vcf do |object, options|
  exit! # Testing to see if this even gets called at all...
end

It seems as if the following code is never even being executed because if I go to /model/123.vcf I get at "Template is Missing" error.

Does anyone know why the ActionController::Renderers.add block doesn't seem to be called?

AuthorsController.rb

respond_to :vcf

def show

  respond_with(@author)

end


Seems like the old style works, executing the renderer format.vcf { render :vcf => @object }.

With respond_with (which now raises "Missing template") you have to add a to_vcf method in the models. Tried for show and it worked (for index it doesn't recognize to_vcf for arrays).

# config/initializers/vcf_renderer.rb
Mime::Type.register 'text/x-vcard', :vcf
ActionController::Renderers.add :vcf do |object, options|
  self.content_type ||= 'text/x-vcard'
  self.response_body  = object.respond_to?(:to_vcf) ? object.to_vcf : object
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜