How do I get multiple paperclip uploads to display in the view? - ruby-on-rails

I'm very new to programming and have followed this screencast at emersonlackey.com on adding multiple paperclip uploads for a record. It all works great but I can't figure out how to display the uploaded images in the records show page.
They display fine on the edit page using:
<%= f.fields_for :venuephotos do |photo| %>
<% unless photo.object.new_record? %>
<p>
<%= link_to image_tag(photo.object.venuephoto.url(:thumb)), photo.object.venuephoto.url(:original) %>
<%= photo.check_box :_destroy %>
</p>
<% end %>
<% end %>
I've used paperclip in other models but they are the standard each record only has one upload so the images display fine with <%= #model.photo.url %> type call but I cant figure out this other way.
Any help is much apprectiated!

You want to do something like this:
<% for asset in #post.assets %>
<%= link_to image_tag(asset.asset.url(:thumb)), asset.asset.url(:original) %>
<% end %>
as per the code that goes along with the screencast.
https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3/blob/master/app/views/posts/show.html.erb

Related

How to make the images from the bootstrap cards clickable on your index pages?

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!

How do I add an image placeholder to fix this bug

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

Adding user's photo to contest, call method with link_to

I came from a JS background so Rails is weird to me. I currently have a show.html.erb for the contest model:
<h1>Contest name: <%= #contest.name %></h1>
<h2>Contest criteria: <%= #contest.criteria %></h2>
<h3>Photos: </h3>
<%= link_to "Enter Contest", "#" %>
<% #contest.photos.each do |photo| %>
<%= image_tag("#{photo}") %>
<% end %>
With the link_to I'm trying to render all photos that belongs to the current_user and pick one of them to assign it to the current contest. The params passing seems all so mysterious to me. Can you guys point me in the right direction of how I should tackle this problem? Thanks
What I understand you need to select an image from list of images and pass the id. So, try this
<%= label_tag "Enter Contest" %>
<% #contest.photos.each do |photo| %>
<%= link_to (image_tag("#{some photo path}")), some_controller_method_path(:photo_id => photo.id) %>
<% end %>
And in controller you can use params[:photo_id]

Issues showing images with paperclip using collections Rails 4

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.

Displaying images from associated models

I'm trying to get my miniatures view to display all associated photos from the collections model.
My sample miniature has two photos but the following code gives an error "undefined method `photo' for #".
<% #miniature.collections(:photo).each do |photo| %>
<%= image_tag #miniature.collections.photo.url(:medium) %>
<% end %>
I think that the relationships are all correct though because rails console works fine with them and the following code shows the first image, twice.
<% #miniature.collections(:photo).each do |photo| %>
<%= image_tag #miniature.collections.first.photo.url(:medium) %>
<% end %>
Equally I can swap out first for last and show the 2nd image twice. What am I doing wrong?
Without knowing a huge amount about your associations, I'm relatively sure you want to use the instance variable that you instantiated in the each loop. That would look like something to the effect of:
<% #miniature.collections(:photo).each do |collection| %>
<% if !collection.photo.url(:medium).nil? %>
<%= image_tag collection.photo.url(:medium) %>
<% end %>
<% end %>
Using #miniature.collections.first.photo.url(:medium) would display the first image of the collection once for each photo in the collection. Likely this is not what you want.

Resources