Rails upload image can't be accessed - ruby-on-rails

I upload an image to the Rails path\public\upload\member\1.jpg
but now I can't access this image.
No route matches [GET] "/public/upload/member/20131030203910.jpg"
So,what should I do?

<%= image_tag "/upload/member/1.jpg" %> try that and also check is it really upload folder or maybe uploads

better you can access the image using object
<%= object.image_url %>

Related

How to display image in rails with ActiveStorage

Rails: 6.1
Following the basic steps in the documentation of ActiveStorage I have added a avatar field for my User model and upload seems to be successful. But when I want to display #user.avatar I get a url for the image but the url seems to be 404. The image is not displayed.
Here is my image tag:
<img src="<%= image_path(url_for(#user.avatar)) %>" alt="Avatar for <%= #user.username %>">
<%= image_tag #user.avatar, class: 'user-image', alt: "Avatar for #{#user.username}" %>
Both of these are failing. Rendered HTML is here:
<img src="/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--569f9d466f5dd0d4917db040f0f87d948c0af667/paddle.png?locale=en" alt="Avatar for Someone">
Looking at the path, paddle.png is the image I uploaded and I assume upload is okay. But why doesn't it display?
Try this:
<%= image_tag #user.avatar.to_s, class: "user-image" %>
Okay, finally I found what was wrong. First thing ActiveStorage is not compatible with UUID models. What's more for a given 12hefhs-342jfsoeif-senfs-senfks UUID it takes the first consecutive digits and uses it as ID. Which is in my opinion a big issue. We don't get any error message like we do for other rails models when we try to save UUID in bigint (default id) field. To overcome this problem I had to add these migrations in this gist
Then the other problem was that I have a regex to redirect all requests to locale version of that same path. Like this:
get "/*path",
to: redirect("/#{I18n.default_locale}/%{path}", status: 302),
constraints: { path: /(?!(#{I18n.available_locales.join("|")})\/).*/ },
format: false
and this was redirecting image urls as well...
Removing this solves the problem. But introduces another one... How to redirect...

Rails image_tag full path

I have images in my assets folder and path to it
"app/assets/images/sets/img_2168.jpg"
so i want to use it like
Dir.glob("app/assets/images/sets/*.jpg") it return me "app/assets/images/sets/img_2168.jpg"
image_tag("app/assets/images/sets/img_2168.jpg") but it gives me empty image
Is it possible if so how to fix it?
Try the followings:
<%= image_tag("sets/" + File.basename(Dir.glob("app/assets/images/sets/*").first))%>
If you just provide "sets/img_2168.jpg" to image_tag, it should work.
You don't need to provide the complete path.
Try this one :
image_tag(File.read("#{Rails.root.to_s + '/assets/images/sets/img_2168.jpg'}").url)
OR
<img src="/assets/images/sets/img_2168.jpg"></img>
Ideally you should pass image object from the controller and in image_tag just get its url like :
image_tag(#image_obj.url)
The easiest way that I have found is to do this: The asset_url tag will put the full url of the asset (in this case an image called: "app-store.png") and then it will be passed to the image_tag normally.
<%= image_tag "#{ asset_url "app-store.png"}" %>
Rails 5 :
<%= image_tag(image_url('sets/img_2168.jpg')) %>
It work for me.

How do I create a default avatar if the avatar file does not exist?

I used paperclip to let users upload their avatars. Everything works fine.
I want to show a default image if the user didn't upload an avatar. I used this code in my view:
<%=
if File.exist?(user.avatar.url)
image_tag user.avatar.url(:large)
else
image_tag "default-avatar.png"
end
%>
but it doesn't show the default image.
I put the default-avatar.png in app/assets/images/.
What am I doing wrong?
EDIT
I followed the instructions oldergod mentioned in comments, but still the uploaded avatars don't show up.
I finally put it to work, using user.avatar? like this:
<%=
if user.avatar?
image_tag user.avatar.url(:large)
else
image_tag "default-avatar.png"
end
%>
I think the problem is that File.exist? needs a path, not a url or something.

Only display a user-uploaded photo if it exists in rails

<%= image_tag message.photo.url(:small) %>
I have paperclip installed and the above code displays an image that the user uploads which has been resized to :small (in this case 40x40px. How can I get my page to display this image only if it exists? Currently if the message includes a photo that photo is displayed but all other messages show broken image links. Thanks
Paperclip adds the name of the attachment suffixed with a "?" to the attached model as a helper method to allow you to see whether or not there is an attachment. In your case, the helper method would be photo? on the message class. You could use it with the tertiary operator in this manner:
<%= message.photo? ? image_tag message.photo.url(:small) : "" %>
Or, if you'd like to show a default no-image image when there is no image...
<%= image_tag message.photo? ? message.photo.url(:small) : url_to_no-image_image %>
It's been a while since I've used rails, but wouldn't it suffice to just put in an if statement?

Rails PaperClip Attachments, knowing if there's a image thumbnail?

I'm using Rails 3 paperclip and allow users to upload attachments to the attachment model.
If the file is an image, the app generates image previews. If the file is not, it only uploads the file (no image previews).
Now I would like to display a list of all the attachments in the DB. So I use attachment.attachment(:large) and that works fine for image attachments, but errors (obviously) for non-image attachments.
What's a good way to check if it's an image attachment or not? If not, I'd like to display a standard static image. Any suggestions? thanks
This is what I did in my view:
<% if !(#attachment.attachment.content_type =~ /^image/).nil? %>
<%= image_tag #attachment.attachment.url(:small) %>
<%end%>
This assumes that your model is attachment, and my file, I so called attachment.
So you could do something like:
<% if !(#attachment.attachment.content_type =~ /^image/).nil? %>
<%= image_tag #attachment.attachment.url(:small) %>
<%else%>
<%= image_tag "/path/to/image/default.png" %>
<%end%>
Check attachment.attachment.attachment_content_type
For example, it might be: "image/jpeg"
You can create a migration that adds a attachment_content_type field of type string to your attachment table. When you create an attachment, paperclip stores the type of the file in that field. You can then check if the file type is something like "image/jpeg".
Maybe you could use default_url option? That would be shown if the real thumbnail doesn't exist.
http://www.suffix.be/blog/default-image-paperclp

Resources