Ruby on Rails Youtube Image Embed - ruby-on-rails

I'm trying to accomplish this without any plugins.
Basically we just use the standard copy and paste method for embedding Youtube onto our website... the problem, then, is that if we try to share a post of ours with video on facebook... the image thumbnail isn't there.
The thumbnail is always saved in this format:
http://i1.ytimg.com/vi/7Uz1hfza55k/default.jpg
...with the video id coming just before "default.jpg"
I already have three "photo" slots in the database for each post.
So I'd like to do something like this:
<%= image_tag("<%= daily.photo .html_safe %>") %>
I'm just not sure of the proper syntax so that it gets the photo URL for that particular post.
What I want it to return in the end is something like this:
<%= image_tag("http://i1.ytimg.com/vi/7Uz1hfza55k/default.jpg") %>
Getting the URL, of course, from the "photo" section of each post's database entry.
For extra credit maybe you could explain a way that I could arrange it so that all the person writing the articles would have to do is enter the video code and it would be automatically inserted in:
<%= image_tag("http://i1.ytimg.com/vi/CODEHERE/default.jpg") %>
Thank you for your time.
Edit:
Just so we're clear this works:
<img src="<%= #daily.photo %>">
But this doesn't work:
<%= image_tag("<%= daily.photo .html_safe %>") %>
They should be the same as far as I know... but they don't work the same. If worse comes to worse I'll just go with img src...

<%= image_tag(#daily.photo) %>
In ERB, <%= stuff %> means: everything inside this is good old plain ruby. There is no need to use the tag twice, as #daily.photo is just an argument for the image_tag method.

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...

Hi, i am quite new to web developing. So forgive me for any stupid question

I am trying to build an ecommerce page using rails. In my products page I want to add products with an image, description, color and price. Everything works properly except for the images. I am not able to assign proper image to the desired product. If i use this syntax : <%= image_tag("/assets/6.jpeg", class: "img-fluid") %>, then this particular image is assigned to all other products and it's definitely not logical!
I have already added the images which I need for my project in the app/assets/images folder. I want to have the possibility to dynamically add/modify the photos on my page.
Please help me how to solve this issue. Thanking you guys in advance!
if images file are saved 1.jpeg-?.jpeg you could loop say
<% #product.each_with_index do |product, index| %>
<% i = index + 1 %>
<%= image_tag("assets/#{i}.jpeg"), class: 'image-fluid' %>
<% end %>
I mean for what you're asking this would work but is probably not the best way to go about this.

Rail 4 image_tag with variable

I am trying to make a show page that displays an image based on the person called out of the database. My current code looks like this :
<%= image_tag('DAF_520x222.jpg') %>
I want to exchange the 'DAF_520x222' portion for a call of #user_name but I am unsure of how to go about putting this in the image_tag itself.
You can directly interpolate it into the string. It will automatically change it to its value
<%= image_tag("#{#user_name}.jpg") %>
This should work

Is there a better way to display the filename in carrierwave?

At the moment
<%= link_to comment.file, comment.file_url %>
displays
/uploads/comment/file/6/IP___Addresses
Is there such thing as something like comment.file.filename ?
Is there a way to get the filename and display a link to that, so it would just say IPAddresses.txt and links to "/uploads/comment/file/6/IPAddresses" ?
Edit:
Figured it out
<%= link_to File.basename(comment.file.url), comment.file_url %>
You could have used the *_identifier method, in your case:
comment.file_identifier

Ruby on Rails pull an id from url, insert into image_tag (gsub?)

I have a twitter feed displaying my tweets on a page. I want to include the thumbnails of my twitpic images as well. I've been using gsub to replace text, but I'm having a hard time figuring out the syntax.
A typical 'twitpic tweet' output with a raw tag will look like this:
This is a tweet with an image. http://twitpic.com/12345
Then the link for the image is
http://twitpic.com/show/thumb/12345.jpg
Here's the simple code to display the text:
<% #tweets.each do |tweet| %>
<li>
<%= raw tweet.text %>
</li>
<% end %>
So I've been experimenting with gsub but am pretty stuck.
I can search for http://twitpic.com/* by /http:\/\/twitpic.com?[^]+ and then take out everything with the url, replacing it with raw html, but this doesn't help if I want to link an image.
If I could extract the twitpic id (http://twitpic.com/12345), I could put that 12345 into a helper method that inputs a string and inserts it into a linked image.
If anyone could give me some insight on how to extract this id, it would be greatly appreciated.
Maybe you can use #split instead of a regex?
"This is a tweet with an image. http://twitpic.com/12345".split("http://twitpic.com/")[1]
If you want to extract id from http://twitpic.com/show/thumb/12345.jpg then try this to fetch the id out of it:
"http://twitpic.com/show/thumb/12345.jpg".scan(/^http:\/\/twitpic.com\/show\/thumb\/(\d+).jpg$/).flatten.first

Resources