31

Right now I am working on rails 3.0.0. If I run my project in terminal, I get this warning. Please help me.

/usr/share/ruby-rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.0/lib/action_dispatch/http/mime_type.rb:98: warning: already initialized constant PDF

ScottJShea
  • 7,041
  • 11
  • 44
  • 67
chandrashekar
  • 609
  • 2
  • 7
  • 16

2 Answers2

62

You might have this in your config/initializers/mime_types.rb file.

Mime::Type.register 'application/pdf', :pdf

It looks like newer versions of rails already registers it.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
23inhouse
  • 1,889
  • 19
  • 18
  • are you sure that rails registers it by default now? – marcgg Aug 23 '12 at 12:36
  • No i'm not sure. It could also be caused by other gems registering it as well. – 23inhouse Aug 24 '12 at 04:21
  • 9
    Yes, sure, it was added on 2011-06-27 in this commit https://github.com/rails/rails/commit/d73269ba53992d8a01e5721aad3d23bc2b11dc4f – mat Mar 13 '13 at 16:34
  • 2
    I had a similar problem. Even though I wasn't registering the :pdf mime type myself, one of the gems I was using was too old and still doing that. When I upgraded the gem to a later version, the error disappeared. If you can't find the source code, likely one of your gems is the culprit. – Amin Ariana Mar 14 '14 at 18:32
  • @AminAriana I had similar case but decided to solve it with re-registering the mime-type. For that I did `Mime::Type.unregister(:csv)` at first in mime-types initializer and after that registered csv-type again with desired new attributes. – Andres May 30 '19 at 06:59
29

Try using lookup_by_extenstion before defining it.

I have this on my config/initializers/mime_types.rb file.

Mime::Type.register "application/pdf", :pdf unless Mime::Type.lookup_by_extension(:pdf)
Alex Pan
  • 4,341
  • 8
  • 34
  • 45
jlucasps
  • 890
  • 7
  • 8
  • 1
    May not be necessary, but it's safer. Thanks! – Eric Wanchic Jul 24 '14 at 19:03
  • I was having the same error in Rails 3.2.14 using the Prawn gem. Changing `Mime::Type.register "application/pdf", :pdf` to `Mime::Type.register "application/pdf", :pdf unless Mime::Type.lookup_by_extension(:pdf)` did the trick! Thanks! – nulltek Oct 23 '14 at 11:47
  • Thanks. This helped me. – Javon Harper Dec 03 '15 at 15:03
  • Simply removing the `Mime::Type.register 'application/pdf', :pdf` gave me errors, but `Mime::Type.register "application/pdf", :pdf unless Mime::Type.lookup_by_extension(:pdf)` did the trick – Matthias Feb 21 '16 at 12:56
  • You might want to extend that a bit to make sure the previous registration is actually identical to the one you are making: Mime::Type.register "application/pdf", :pdf unless Mime::Type.lookup_by_extension(:pdf) == "application/pdf" – Ed Allen Jun 14 '17 at 19:20