I am trying to have a link to a pdf file on a website. When someone clicks on the link, it should open the pdf file up in a new browser window.
I have the following code in the view:
<% #filtered_portfolio_files.each do |the_file| %>
<li>
<% if the_file.is_picture? %>
<%= image_tag(the_file.port_file.picture) %>
<% else %>
<%= link_to image_tag('/assets/pdf_image.png'), the_file, target: '_blank' %>
<% end %>
</li>
<% end %>
This should show the image if the file is an image or a dummy picture of the Adobe PDF logo that links to the pdf file if the file is a PDF.
Right now this loop goes through two files. The first is an image and the 2nd is a PDF. It correctly shows the first image and the 2nd correctly shows an image of the PDF logo, but when clicked on it brings me to a page that says
No route matches [GET] "/portfolio_files/4"
I have also tried in the view:
<% #filtered_portfolio_files.each do |the_file| %>
<li>
<% if the_file.is_picture? %>
<%= image_tag(the_file.port_file.picture) %>
<% else %>
<%= link_to image_tag('/assets/pdf_image.png'), the_file.port_file, target: '_blank' %>
<% end %>
</li>
<% end %>
but that stops the page from loading and returns the error:
undefined method `model_name' for PortfoliofileUploader:Class
How do I get the PDF link to correctly link to correctly link to the pdf file?
<%= link_to image_tag('/assets/pdf_image.png'), the_file.port_file.url, target: '_blank' %>
Add a .url on the end.
Related
I was using the Bootstrap cards for my index page for my app but wasn't really liking the event details button, I feel it's more modern to just click on the image.
I found SOF articles pointing to how to use clickable images if the image was local and/or hosted somewhere online, but nothing that pointed to using images that are already a part of your database.
The code I had before was:
<% if artist.avatar.attached? %>
<%= image_tag artist.avatar, class: "even-size-artist" %>
<% else %>
<img src=<%= "https://dancewise.s3.amazonaws.com/misc-images/Blank+Avatar.png" %> class="even-size-artist">
<% end %>
The code that I found that will give you the option to have the top image clickable to the appropriate record is as follows:
<% if artist.avatar.attached? %>
<%= link_to image_tag(artist.avatar, class: "card-img-top artist-img").html_safe, artist %>
<% else %>
<img src=<%= "https://dancewise.s3.amazonaws.com/misc-images/Blank+Avatar.png" %> class="card-img-top artist-img">
<% end %>
Hopefully, this helps someone else out there learning RoR!
I am a beginner programmer. I'm building my first project which shows a list of my favorite athletes and when clicked the "show" page loads an image of them, their name, and weight.
Overall when adding a new athlete everything works fine. If I decide to add a new athlete and skip adding an image I get this error "Can't resolve image into URL: undefined method `persisted?' for nil:NilClass", it also highlights my code "<%= image_tag(#fighter.image) %>" that's in my show.html.erb
I've tried adding an alt: but I get another error of "wrong number of arguments (given 1, expected 0)".
I've searched for answers and one thread mentioned my controller page and the params section. I did notice that (:image) is preceded by "params.require" which I would assume could be the problem if I'm not adding an element that is required. How do I solve this issue? What would be the best way to add a placeholder, I've added a default.png in my images folder.
Thank you
show.html.erb
fighters_controller.rb
If you're using ActiveStorage for uploading athlete images. You can check if an image was attached to your model with #fighter.image.attached?.
So I would suggest wrapping the image_tag in an IF-clause like so:
<% if #fighter.image.attached? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
If you're using Paperclip then try the following:
<% if #fighter.image.exists? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
With Carrierwave use:
<% if #fighter.image.present? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
I am trying to insert image in student report, edited the below code and it shows the default image but when I upload the student image it doesn't show it.
<% if #student.photo.file? %>
<%= wicked_pdf_image_tag #student.photo.url %>
<% else %>
<%= wicked_pdf_image_tag "master_student/profile/default_student.png" %>
<% end %>
I am having an issue getting a collection of user posted pics to show using paperclip. From the code you'll see that the app allows either a link or a file to be shown. It Works just fine showing all on pics/index but the papercliped files won't show in the user collection users/1/pics. I believe it has something to do with the way Rails puts the path in the image_tags.
Routes
resources :users do
member do
get :pics
get :toggle_approve
end
User/show
<% if #user.pics.any? %>
Pics Posted: <%= link_to #pics.count, pics_user_path(#user) %>
<br />
<% else %>
No Pics Posted!<br />
<% end %>
Pics/index
<% #images.each do |image| %>
<% if !image.image_link.nil? %>
<li><%= link_to image_tag(image.image_link, size:"100x200"), pic_path(image), class: "th" %><br>
<% else %>
<li><%= link_to image_tag(image.image.url(:large), size:"100x200"), pic_path(image), class: "th" %><br>
<% end %>
<% end %>
EDIT
I figured it out. The users view for the pics /user/1/pics had similar code as the pics/index. When I added the functionality for the paperclip uploaded file, I didn't update the user pics view with the same logic to determine if it was a link or a file.
My app showing thumbnails, but if I click the thumbail, its downloaded, not showed up. How its work? link_to image_tag('lorem'), image_tag('lorem_big')?
<% for photo in #article.attachments %>
<%= link_to image_tag('dinamic/'+photo.id.to_s+'/'+'thumbs_'+photo.image_file_name), "#{request.env["HTTP_HOST"]}/public/images/dinamic/"+photo.id.to_s+"/"+"originals_"+photo.image_file_name, :class => 'single_image'%>
<% end %>
Have you tried different browsers?
Try to capture HTTP communication how your app server sets Content-Disposition header attribute.
If you cannot control your server, you can do minicontroller which will render one tag with that image :)
<% for photo in #static.attachments %>
<a id="single_image" href="<%= image_path("dinamic/"+photo.id.to_s+"/"+"originals_"+photo.image_file_name) %>" > <%= image_tag('dinamic/'+photo.id.to_s+'/'+'thumbs_'+photo.image_file_name) %> </a>
<% end %>
Image_path solved this issue.