Displaying articles with tag mime types

Ruby on Rails :: Default mime types

Posted by PunNeng, Sun Feb 24 15:55:00 UTC 2008

ต้นกล้ามาถามว่า เวลาใช้ respond_to แล้วเกิดรับ format ของ mime type ที่ไม่ได้เซ็ตไว้ มันก็จะทำงานไม่ถูกต้อง ผลที่ได้คือ จะได้ error 406 Not Acceptable
วิธีซ่อมง่ายๆ จากการแงะ plugin ที่ชื่อว่า exception_notifier คือใช้ format.all

  1
  2
  3
  4
respond_to do |format|
  format.html 
  format.all { redirect_to :action => "index" }
end

ถ้าไม่ใส่ block เพิ่มไป มันจะไปเรียก index.all.erb ในกรณีที่ใช้ restful
แต่ถ้าไม่ใช้ ก็จะเป็น html ไป

ก็มาสงสัยอีว่า default mime type ที่เซ็ตไว้มีอะไรบ้าง ก็ไปแงะมา เจอ

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
# Build list of Mime types for HTTP responses
# http://www.iana.org/assignments/media-types/

Mime::Type.register "*/*", :all
Mime::Type.register "text/plain", :text, [], %w(txt)
Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml )
Mime::Type.register "text/javascript", :js, %w( application/javascript application/x-javascript )
Mime::Type.register "text/css", :css
Mime::Type.register "text/calendar", :ics
Mime::Type.register "text/csv", :csv
Mime::Type.register "application/xml", :xml, %w( text/xml application/x-xml )
Mime::Type.register "application/rss+xml", :rss
Mime::Type.register "application/atom+xml", :atom
Mime::Type.register "application/x-yaml", :yaml, %w( text/yaml )

Mime::Type.register "multipart/form-data", :multipart_form
Mime::Type.register "application/x-www-form-urlencoded", :url_encoded_form

# http://www.ietf.org/rfc/rfc4627.txt
Mime::Type.register "application/json", :json, %w( text/x-json )

ถ้าจะเพิ่ม ก็ไปประกาศได้ที่ environment.rb ว่า

  1
Mime::Type.register "image/jpg", :jpg

0 comments | Filed Under: Ruby on Rails | Tags: mime types

codegent: we're hiring