Adding custom MimeTypes into rails format


/ Published in: Rails
Save to your folder(s)



Copy this code and paste it in your HTML
  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!

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.