2

I am using carrierwave for uploading images(jpg,jpeg) as well as pdf, doc, xls, files in my application.

so when i use

<%= link_to (image_tag media.image_url().to_s) %>

or

<%= image_tag media.image_url().to_s %>

It shows broken image for (pdf, doc,xls) & when i right click on that broken image, It opens save image as box & saves that (doc, pdf, xls) for me.

but all i want is that it should show default image for (pdf,doc,xls) & just clicking on that should open popup of save file.

Using:

  1. carrierwave (0.5.7)
  2. Rails (3.0.10)
chaitanya
  • 1,974
  • 3
  • 17
  • 35

2 Answers2

2

You mean a default image for pdf, doc and xls, not a preview? So you probably want something like:

<%= link_to(image_tag(preview_or_default_image(media)), media %>

and then define a helper:

def preview_or_default_image(media)
  case media.type
  when 'jpg', 'jpeg' # , 'png', ...
    media.image_url
  when 'doc', 'docx'
    'path/to/doc/default_image.png'
  when 'xls', 'xlsx'
    'path/to/xls/default_image.png'
  when 'pdf'
    'path/to/pdf/default_image.png'
  end
end
Mario Uher
  • 12,249
  • 4
  • 42
  • 68
  • Instead of `media` (not sure where that is being defined above), I used: `model.uploader.file.extension` as shown here: http://stackoverflow.com/questions/11644557/carrierwave-how-to-get-the-file-extension – Bradley Marques Apr 28 '15 at 12:55
  • Media is the carrierwave object, refer to question above ;) – Mario Uher Apr 28 '15 at 14:34
0

For more information about CarrierWave, checkout the Railscast by Ryan Bates.

Igbanam
  • 5,904
  • 5
  • 44
  • 68