Ruby displaying "InvalidURIError" when certain string is put through regex - ruby-on-rails

I'm attempting to grab the URL of every image within a directory and display them with image_tag(s), but Ruby was appending "images/" in front of the image src and made them not work.
I tried to rectify this by removing everything before the name of the subdirectory in the images folder but it caused an error and only with that one specific word.
Using HAML
#models = Dir.glob("app/assets/images/creation/model/*.png")
#i = 0;
- #models.each do |model|
- #i++
%td{:id => "#{#i}"}
%img= image_tag model.gsub!(/.*?(?=creation)/im, "")
Inserting "creation" breaks it, inserting seemingly anything else is fine.
Was there something I did wrong?
Update: I tried renaming the folder and changing the code accordingly, same thing happened. Inserting the name of the folder with one character changed doesn't display an error and renders nothing, but as soon as you put in the actual name you get "InvalidURIError"

Upon taking a look at the error I realized that Rails was taking issue with a single image's filename, I guess it had characters in it that Rails didn't like.
Weird, I've used the same image in a PHP application fine.

Use this code:
%img= image_tag URI.encode("creation/model/#{model.split('/').last}")

Related

Image not showing in Rails 4.1.1

I'm following a tutorial: http://tutorials.jumpstartlab.com/projects/merchant.html, where we call product images like this:
<%= image_tag "products/#{product.image_url}" %>
That should work, but I get this 404 error, and this shows up in the console:
GET http://localhost:3000/images/products/%20purple_grapes.jpg
What is that %20 in there. When I hard code the image name, it works. Rails is looking in the right place for the image, which is /assets/images/products/ but the file name should be purple_grapes.jpg, not %20purple_grapes.jpg
This problem is driving me batty. I've done this kind of code before calling images and not had this problem. Any idea why this isn't working? I read through The Assets Pipline and according to it this code is correct.
Thank you!
You probably just entered a space before your file name in the text field.
To avoid this, you can add a validator to your field or force strip on it to avoid leading/ending whitespace.

How to make Carrierwave add random string to the file path?

I have this kind of hot fix:
= image_tag "#{current_candidate.profile.photo_url}?stamp=#{SecureRandom.hex}"
The reason for this is that I made a "crop" feature, and when users recrop their photo - old version gets displayed from browser cache.
Anyway.
This photo_url is called in many places, and rather than making a helper & call it everywhere I thought it would be nice to configure uploader to do that.
Is that possible?
Note: filename can't be random. For other reasons.
I would imagine it has something to do with overriding photo_url method.

How to download image from url and display in view

I am trying to download an image and displaying it in a view in rails.
The reason why I want to download it is because the url contains some api-keys which I am not very fond of giving away.
The solution I have tried thus far is the following:
#Model.rb file
def getUrlMethod
someUrlToAPNGfile = "whatever.png"
file = Tempfile.new(['imageprependname', '.png'], :encoding => "ascii-8bit")
file.write(open(data).read)
return "#{Rails.application.config.action_mailer.default_url_options[:host]}#{file.path}"
end
#This seems to be downloading the image just fine. However the url that is returned does not point to a legal place
Under development I get this URL for the picture: localhost:3000/var/folders/18/94qgts592sq_yq45fnthpzxh0000gn/T/imageprependname20130827-97433-10esqxh.png
That image link does not point anywhere useful.
My theories to what might be wrong is:
The tempfile is deleted before the user can request it
The url points to the wrong place
The url is not a legal route in the routes file
A am currently not aware of any way to fix either of these. Any help?
By the way: I do not need to store the picture after I have displayed it, as it will be changing constantly from the source.
I can think of two options:
First, embed the image directly in the HTML documents, see
http://www.techerator.com/2011/12/how-to-embed-images-directly-into-your-html/
http://webcodertools.com/imagetobase64converter
Second, in the HTML documents, write the image tag as usual:
<img src="/remote_images/show/whatever.png" alt="whatever" />
Then you create a RemoteImages controller to process the requests for images. In the action show, the images will be downloaded and returned with send_data.
You don't have to manage temporary files with both of these options.
You can save the file anywhere in the public folder of the rails application. The right path would be something like this #{Rails.root}/public/myimages/<image_name>.png and then you can refer to it with a URL like this http://localhost:3000/myimages/<image_name>.png. Hope this will help.

My app cannot load some images from the server

I'm using Ruby on Rails on my local WEBrick server.
I'm generating some images of websites from urls, writing them to my local filesystem, and displaying them. I name the images by their url but replace all \ with -. Some images seem to not be loading because it cannot find the images on my filesystem, and I get the broken image icon. However, I see that all the images are there when I check my filesystem.
This is the error I get in my logs:
Started GET "/images/image_site/http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg?1309465379" for 127.0.0.1 at Thu Jun 30 13:23:06 -0700 2011
ActionController::RoutingError (No route matches "/images/image_site/http:--www.urbandictionary.com-define.php"):
This is my html code:
<img alt="Http:--www.urbandictionary.com-define.php?term=slim%20shady" class="site_image" src="/images/image_site/http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg?1309465379">
What going on and how can I fix this? Please let me know if I need to provide more information.
Looks like you're not properly encoding your image names in the src attribute. I'd guess that you have a file with a name like this:
http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg
But when you have this:
src="/images/image_site/http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg?1309465379"
The filename looks like this:
/images/image_site/http:--www.urbandictionary.com-define.php
because everything after the first ? is considered to be part of the query string.
Replacing the slashes with hyphens is not good enough, you're still leaving all sorts of holes for confusion and nefarious intent. Instead, you should generate the image file names completely, something like id.jpg where id is the image's ID in your database. Then, store the original filename in your database and only show that filename (properly encoded!) to people, don't use it in your local file system.
A quick fix would be to properly URL encode your src attributes. But you really should fix up how you handle the images or you will leave yourself open to all sorts of trouble.
You need to place images in your public/ folder and then you can access them from there. By default Rails will server static assets primarily from public/.

file_field_tag : what was the original file name?

My site allows users to upload csv files for processing. It all works
fine, but on the response I'd like to report something like "Your file
abc.csv processed OK".
Unfortunately I cannot seem to find the actual original file name in
the params, even though Firebug tells me it's part of the post.
Any tips?
Thanks....
Try using debug on the results of your form.
http://guides.rubyonrails.org/debugging_rails_applications.html#debug
As Jarrod mentions in the comments above. Use params[:file].original_filename
Funny thing is, my form has two file upload tags (file1 and file2). One comes in as a ActionController::UploadedTempfile and the other ActionController::UploadedStringIO.
This may be a rails bug but it doesn't matter to me as both have the original_filename method.

Resources