在Rails中添加自定义格式(与respond_to一起工作)[duplicate]

时间:2021-12-08 01:20:28

This question already has an answer here:

这个问题已经有了答案:

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:

我有地图。参考资料:post和我希望能够以markdown格式为post主体提供服务。所以我设置了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:

但是当我尝试访问/贴子/1234。markdown,我得到这个错误:

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?

如何将markdown添加为可接受的格式?在哪里可以看到可接受格式的列表?

2 个解决方案

#1


33  

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

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


2  

Mime::Types.register

#1


33  

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

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


2  

Mime::Types.register