2

Please be patient with me as I am very new to Rails and this is my first question on StackOverflow.

I have searched this question in many different ways and have yet to find a solution that will work for me. I have a feeling I am close but perhaps my syntax is off a little bit, or i should be trying to call a different method. any help would be appreciated.

Anyways, onto my problem.

I am creating a web application that will allow users to upload photo albums. I have the Users/Albums/Categories/ etc.. all working fine. When a guest selects a users profile they are presented with the albums tied to/ created by that user. if the guest then selects the album they are then taken to the album_show page which shows all of the images in the album (sized at 200x200) which is working exactly as it should. What i cannot get to work is that I want a guest to be able to click on one of the photos in the album and have a new tab open with the full size image. I am currently trying to use this

#album_show
<% @album.images.each do |image| %>
  <%= link_to image_tag(image.variant(resize: "200x200")), path_to :image, target: :_blank %>
<% end %>

Ive tried subbing out the " path_to :image)," and replacing it with a website like "google.com" and it works as would be expected. clicking on an image opens a new tab at google.com" (i did this just to see if the rest of the code was working properly)

so what I need to know is what do i put instead of " path_to :image," in order to open a full size version of the image clicked on, in a new tab?

thank you in advance for your help, and I am aware this is a noob question haha

  • are the images saved in DB as blob? or save on the server? Can you post the HTML of one of your image row in the show page? – Fernand Jan 21 '20 at 06:30
  • @Fernand the images are saved as BLOBS. Kamal gave me the solution I needed. Thanks for taking the time to comment though. – Tyler Trudeau Jan 21 '20 at 17:57

2 Answers2

2

There are few ways to do it. You can use following line instead of your link_to link. This is exactly you wanted to do.

      <%= link_to image_tag(image.variant(resize: "200x200")), image %>

Another way of doing it using href.

<a href="<%= url_for image %>"><%= image_tag(image.variant(resize: "200x200")) %></a>

This will solve your issue.

Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37
  • that first line is exactly what I needed, thank you. I could have sworn I tried that very first, but perhaps I had a typo and didnt notice then assumed that didnt work. I did add ", target: :_blank" onto the end so that it opens into a new tab though. – Tyler Trudeau Jan 21 '20 at 18:02
  • KamalPanhwar - I do have one more quick question. When I click my image and it opens into a new tab, how do I edit what gets shown in that tab that was opened? for example if I didnt want the full size image but say 600x600 or if I wanted to add styling to the page or add code for ads etc.. Thanks in advance – Tyler Trudeau Jan 21 '20 at 18:05
  • Same as you did use variant change it to ` <%= link_to image_tag(image.variant(resize: "200x200")), image.variant(resize: "2000x2000") %> ` it will open now 2000X200 size. you can change sizes as you want. – Kamal Panhwar Jan 22 '20 at 05:25
0

This is what worked for me in rails 7

<%= link_to "View photo", rails_blob_url(@user.photo) %> 

If you want it to open in a new tab:

<%= link_to "View photo", rails_blob_url(@user.photo), target: :_blank %> 
stevec
  • 41,291
  • 27
  • 223
  • 311