Add a custom format in Rails (that will work with

2019-02-11 16:31发布

问题:

This question already has an answer here:

  • Custom formats in Ruby on Rails 1 answer

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?

回答1:

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

# add in config/initializers/mime_types.rb
Mime::Type.register "text/markdown", :markdown


回答2:

Mime::Types.register