A rails3 application with Carrierwave and MiniMagick is loading files. However in an attempt to get meta-information out it is hitting Errno::ENOENT
The helper method
def image
#document = Document.find(params[:id])
image = MiniMagick::Image.open(#document.production_file)
end
is called in the view
<%= image['width'] %>
but the error is specified as
No such file or directory - /uploads/document/production_file/1/leader_pg_600.jpg
Yet
localhost:3000/uploads/document/production_file/1/leader_pg_600.jpg
does show the file, and is identical to
<%= #document.production_file_url %>
why is this routing apparently not being seen?
ImageMagick is installed and the application is running a resizing manipulation.
Accessing the wrong form of information...
image = MiniMagick::Image.open(#document.production_file)
is only a string.
Should read
image = MiniMagick::Image.open(#document.production_file.path)
Related
i have a problem i am deveoping a application using rails 2.3.8 . my problem is with the pdf. when a generated the pdf it shows all images in Linux.(ubuntu)
but the same code when i tried in windows7 the pdf is not showing the images in the body.the header and footer images are loading. the header and footer in the layout. i am using wickedpdf for pdf generation.
my code is
<%= wicked_pdf_image_tag "#{Rails.root}/public/images/master_student/profile/default_student.png" ,:width=>85,:height=>100 %>
the code working fine in the ubuntu but not working in the windows7..
please help
I just overcame a similar issue on Windows with images uploaded through Paperclip, because WickedPDF's wicked_pdf_image_tag helper requires the images to be in public/images.
I used the solution given here for that problem, but I still couldn't get the images to render in the PDF. Then I discovered that if I put the image into the public/images folder and referenced it that way, the wicked_pdf_image_tag helper rendered the image tag with forward slashes instead of backslashes after the 'file://'.
This is the helper method I ended up with, which replaces wicked_pdf_image_tag:
module ApplicationHelper
def wicked_pdf_image_tag_for_public(img, options={})
if img[0] == "/"
# Remove the leading slash
new_image = img.slice(1..-1)
image_tag "file:///#{Rails.root.join('public', new_image).to_s.gsub("/", "\\")}"
else
image_tag "file://#{Rails.root.join('public', 'images', img).to_s.gsub("/", "\\")}}", options
end
end
end
I'm using rails 3.0 and PDFKit. SASS and HAML but I don't have the asset pipeline implemented yet.
If I make a call from a controller I can generate perfect styled pdf with images, calling PDFKit.new passing render_to_string :show.
But if I do the same through a rake task, my PDF is generated without styles, and image_tag helper throws an error like this:
can't convert nil into String
Surely, I'm doing something wrong in the rake task... but everything works in the controller... What I'm missing?
Should I include something in the rake task? or maybe use another view with inline styles and absolute paths?
the calls are these:
CONTROLLER VERSION
def generate_html_invoice
render_to_string :show, layout: 'mypdflayout'
end
mypdf = PDFKit.new html_generator
RAKE TASK VERSION
def generate_html_invoice
invoice_view = ActionView::Base.new(MyWeb::Application.config.paths["app/views"].first)
invoice_view.assign({ ....... various params here})
html_invoice = invoice_view.render(template: "invoices/show", layout: 'mypdflayout')
return html_invoice
mypdf = PDFKit.new html_generator
The same error is thrown by image_tag helper and stylesheet_link_tag helper
An alternative way could be instantiate the controller in the rake task but.. is it possible? and, is it a good practice?
I couldn't find any decent solution to this, but I tried the following techniques:
QUICK AND DIRTY
change the view using %link and %img HAML tags instead the helpers, using the absolute path to the files.
SLOW BUT ELEGANT
In the rake task, call the controller to receive the view url and give it to PDFKit in this way
url = "#{(Rails.env.production? ? 'http://www.example.com' : 'http://localhost')}/invoce/#{invoice.id}"
path_to_pdf = "root/......./mypdf.pdf"
invoce_page = PDFKit.new url
invoce_page.to_file(path_to_pdf)
This one is the solution I choose. I know, it's a little dumb: the controller calls a rake task which calls the controller again... And it makes a lot of http request to the server.
But in this way I can have a underground process to generate PDF invoices without waiting for the response.
I think I don't have to worry about overcharging the server because the request will be queued normally.
I've encountered a problem with displaying images in pdf's and html's.
Basically what I want is to make my html view be downloadable in pdf. So I've created a partial _pdf_view.html.haml, which looks (for now) like so:
%p
= wicked_pdf_image_tag "vehicles/Toyota_Echo1-1.jpg"
then I simply render this partial both in download.html.haml and in download.pdf.haml. The idea is that the html file previews what is going to be downloaded as a pdf.
The funny part is that this code will put the image nicely in my pdf, but not in my html. In PDF there needs to be specified full file path, however, in html that won't fly. If I change the path to "/vehicles/Toyota_Echo1-1.jpg" then it is the other way around...
Any way around it, besides creating separate files or separate chunks of code just for images?
P.S I'm using: rails 3.2.12, wkhtmltopdf 0.9.9, wicked_pdf 0.9.5
Change the image tag created based on the :format.
- if params[:format] == 'pdf'
= wicked_pdf_image_tag 'vehicles/Toyota_Echo1-1.jpg'
- else
= image_tag 'vehicles/Toyota_Echo1-1.jpg'
I have a validation in my Rails (3.1.4) model to make sure no one tries to upload anything malicious in leiu of their profile image, but when I try to upload a jpeg, the validation is triggering. I'm using the Paperclip gem and I'm unsure if that is having an impact.
validation in user.rb model
validates_attachment_content_type :profile_image, :content_type => ['image/jpeg', 'image/png', 'image/gif'], :message => "Only jpeg, gif and png files are allowed for profile pictures"
When I look at the properties of the jpeg locally (Windows O/S):
Item type = JPEG image
Type of file: JPEG image (.jpg)
Am I doing something wrong in my validation?
Also, when it triggers, it puts the model and field name before the message. Is there a way to avoid that? ie 'Profile image profile image content type Only jpeg, gif and png files are allowed for profile pictures'
Thank you!
You should add 'image/jpg' to the content type array, I think that's what you're missing.
I'm trying to setup an app where I can upload .mp3 samples and play them back. Paul Irish recommends MediaElementPlayer, so I've started futzing around with it. I happily see that there is a gem available (Thank you Axel K.), but I just can't seem to get it working.
I've included the gem, and the asset pipeline seems to be loading everything, but when the page loads, there is a player for each song, but it shows 0:00 on each end and when I press play, nothing happens. The path to the songs are all correct as well.
Here is the code from my view:
<% #songs.each do |song| %>
<%= song.id %>: <%= song.title %><br/>
<audio id="player<%= song.id %>" src="<%= song.tune.path %>" type="<%= song.tune_content_type %>" controls="controls">
</audio>
<% end %>
<script>
$('audio').mediaelementplayer();
</script>
I copied the format and the audio tags directly from the MediaElementPlayer site example (which could be the problem, but I'm not seeing how...)
Also potentially of import, I'm using Paperclip to upload the .mp3s.
**UPDATE:
I just dropped a song into a public folder of my app and setup and audio tag for it, and it works. This makes me think there is something wrong with the way that songs are being uploaded. But all the info seems to be correct (path, content-type, etc...)...
So, I went through and just configured Amazon S3 (which is what I'll be using in production), and everything works without a hitch. There has to be something with where the file is stored locally, or how it's stored, or, heck, I don't know. But setting up S3 and storing the clips there works like a dream.