Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

update post

  • Loading branch information...
commit edf5203d68ebb060b3f800acc6910339b3b02e9a 1 parent 88b7401
harithe authored
40 _posts/2011/2011-04-01-customrize-csv-renderer-in-rails3.markdown
Source Rendered
@@ -61,6 +61,46 @@ add :xml do |xml, options|
61 61 end
62 62 {% endhighlight %}
63 63
  64 +在上面的方法里,直接使用了to_xml方法返回对象的xml形式。
  65 +
  66 +`self.content_type ||= Mime::XML`指定了HTTP响应的Mime types,在`mime_types.rb`文件中,定义了所有在Rails中可以使用的Mime types:
  67 +{% highlight ruby %}
  68 +# Build list of Mime types for HTTP responses
  69 +# http://www.iana.org/assignments/media-types/
  70 +
  71 +Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml )
  72 +Mime::Type.register "text/plain", :text, [], %w(txt)
  73 +Mime::Type.register "text/javascript", :js, %w( application/javascript application/x-javascript )
  74 +Mime::Type.register "text/css", :css
  75 +Mime::Type.register "text/calendar", :ics
  76 +Mime::Type.register "text/csv", :csv
  77 +
  78 +Mime::Type.register "image/png", :png, [], %w(png)
  79 +Mime::Type.register "image/jpeg", :jpeg, [], %w(jpg jpeg jpe)
  80 +Mime::Type.register "image/gif", :gif, [], %w(gif)
  81 +Mime::Type.register "image/bmp", :bmp, [], %w(bmp)
  82 +Mime::Type.register "image/tiff", :tiff, [], %w(tif tiff)
  83 +
  84 +Mime::Type.register "video/mpeg", :mpeg, [], %w(mpg mpeg mpe)
  85 +
  86 +Mime::Type.register "application/xml", :xml, %w( text/xml application/x-xml )
  87 +Mime::Type.register "application/rss+xml", :rss
  88 +Mime::Type.register "application/atom+xml", :atom
  89 +Mime::Type.register "application/x-yaml", :yaml, %w( text/yaml )
  90 +
  91 +Mime::Type.register "multipart/form-data", :multipart_form
  92 +Mime::Type.register "application/x-www-form-urlencoded", :url_encoded_form
  93 +
  94 +# http://www.ietf.org/rfc/rfc4627.txt
  95 +# http://www.json.org/JSONRequest.html
  96 +Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest )
  97 +
  98 +Mime::Type.register "application/pdf", :pdf, [], %w(pdf)
  99 +Mime::Type.register "application/zip", :zip, [], %w(zip)
  100 +
  101 +# Create Mime::ALL but do not add it to the SET.
  102 +Mime::ALL = Mime::Type.new("*/*", :all, [])
  103 +{% endhighlight %}
64 104 所以,我们可以在controller中使用下面的方式来render一个xml:
65 105 {% highlight ruby %}
66 106 render :xml => @obj

0 comments on commit edf5203

Please sign in to comment.
Something went wrong with that request. Please try again.