将自定义mimetype添加到rails格式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将自定义mimetype添加到rails格式相关的知识,希望对你有一定的参考价值。

  1. #in config/initializers/mime_types.rb, declare
  2. Mime::Type.register "device/zzz", :zzz
  3.  
  4. ......
  5.  
  6. request.format => "device/zzz"
  7.  
  8. ......
  9.  
  10. respond_to do |format|
  11. format.zzz { puts "abcabcabcabcabc"}
  12. end
  13.  
  14.  
  15. ===============================================================================================================
  16.  
  17. In a rails3 app (3.0.5), I've declared the following mime-type (in config/initializers/mime_types.rb):
  18.  
  19. Mime::Type.register_alias "text/html", :print
  20. In orders_controller.rb, I've the following action:
  21.  
  22. respond_to :html
  23.  
  24. def show
  25. @order = Order.find(params[:id])
  26. respond_with @order do |format|
  27. format.print
  28. end
  29. end
  30. Then I currently have 2 identical views corresponding to the html and print format:
  31.  
  32. show.html.haml:
  33.  
  34. = @order.name
  35. show.print.haml:
  36.  
  37. = @order.name
  38. Everything works as expected with the 'html' path, ie /orders/2 renders the shows the name of the order with id == 2, but if I try /orders/2.print, I get a
  39.  
  40. undefined method 'user' for nil:NilClass
  41. as if the @order instance variable isn't passed to the 'print' view. What am I missing? Any ideas? It should be trivial, but I'm stuck on this since a few hours!

以上是关于将自定义mimetype添加到rails格式的主要内容,如果未能解决你的问题,请参考以下文章

如何将自定义 RESTful 路由添加到 Rails 应用程序?

将自定义javascript添加到nested_form(Rails)

如何将自定义列添加到数据表?

如何将自定义视图+控制器添加到模型?

使用冰山表格式将自定义元数据添加到 DataFrame 模式

php 将自定义按钮添加到TinyMCE以在Wordpress上创建自定义格式