19

I have map.resources :posts and I want to be able to serve post bodies in markdown format. So I set up my respond_to block:

respond_to do |format|
  format.markdown {
    render :text => @post.body.to_s
  }
end

But when I try to access /posts/1234.markdown, I get this error:

NameError (uninitialized constant Mime::MARKDOWN):
  app/controllers/posts_controller.rb:96:in `show'
  app/controllers/posts_controller.rb:79:in `show'

How do I add markdown as an acceptable format? Where can I see the list of acceptable formats?

Tom Lehman
  • 85,973
  • 71
  • 200
  • 272

2 Answers2

35

http://weblog.rubyonrails.org/2006/12/19/using-custom-mime-types

# add in config/initializers/mime_types.rb
Mime::Type.register "text/markdown", :markdown
Taylor Williams
  • 256
  • 4
  • 22
clyfe
  • 23,695
  • 8
  • 85
  • 109
  • 27
    Probably `config/initializers/mime_types.rb` might be the appropriate place for registering a new MIME type. – Harish Shetty Mar 16 '10 at 18:23
  • the link has been removed – baash05 May 23 '13 at 00:05
  • 2
    The link is actually broken. You can find the article at http://weblog.rubyonrails.org/2006/12/18/using-custom-mime-types/ – yagooar Jun 02 '13 at 11:27
  • 3
    @HarishShetty 's comment is an essential part of this answer. Please update your answer -- current docs suggest `config/initializers/mime_types.rb` -- http://guides.rubyonrails.org/action_controller_overview.html#restful-downloads – Don Cheadle Feb 09 '15 at 16:25
1
Mime::Types.register
yfeldblum
  • 65,165
  • 12
  • 129
  • 169