Image not showing in Rails 4.1.1 - ruby-on-rails

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.

Related

How can I put a link inside of a text string in HAML?

This should probably be easier than it is. I just want to put a link inside an HTML paragraph element.
%p{class: "answer"}="Please upload your data to this portal in text (tab-separated) format. Download our template #{raw(link_to 'here', '/templates/upload_template.xlsx')} for sample data and a description of each column."
Rails is encoding the tag information. I don't want tags to be encoded. I want them to be tags.
You can use more than one line inside any block, to solve your problem we will have something like this:
%p{class: "answer"}
Please upload your data to this portal in text (tab-separated) format. Download our template
= link_to 'here', '/templates/upload_template.xlsx'
for sample data and a description of each column."
You can use interpolation directly in Haml, and doing this seems to fix the issue in this case.
So instead of doing this:
%p= "Text with #{something_interpolated} in it."
you can just do
%p Text with #{something_interpolated} in it.
i.e. you don’t need the = or the quotes, since you are just adding a single string. You shouldn’t need to use raw either.
(Also, note you can do %p.answer to set the class attribute, which may be cleaner if the value isn’t being set dynamically.)
Why this is being escaped here is a different matter. I would have expected the two cases (%p= "#{foo}" and %p #{foo}) to behave the same way. However, after a bit of research, this behaviour seems to match how Rails behaves with Erb.

Ruby displaying "InvalidURIError" when certain string is put through regex

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}")

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.

How do I replace an attachment in an email with the Rails mail gem?

My program is dealing with emails which arrive as files (something.eml). In some circumstances I need to amend an attachment and then resave the file. I've been using the instructions here as a basis for my code, but there's no suggestion for trying to do exactly what I'd like. The code I have below successfully removes the original attachment and then tries to add in a new one.
#email.without_attachments!
#email.add_file(amended_version)
Unfortunately it goes wrong in two places. Firstly it seems to remove all mime parts, not just attachments. Any text/plain sections are also ditched. Secondly, if I test by reloading my amended .eml file, the attachment is no longer recognised, despite being in the file.
I've included a gist which includes the original and amended files from my current method.
Is there a better way to do this? Perhaps a way of replacing the attachment directly rather than get rid of it and adding again?
I don't know enough about mail formatting to know why this works, but it does.
I extracted just the line I was interested in from the without_attachments! method and it now seems to work fine. The non-attachment parts of the message are kept intact and the message re-reads fine. Code now reads....
#email.parts.delete_if { |p| p.attachment? }
#email.add_file(amended_version)

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