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.
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 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]
So I have an object that has multiple photos (through use of a model called asset). I'm using paperclip to upload the photos and it's working fine. On my show page I call
<li class="grid_3">Photos:<br /></li>
<% #address.assets.each do |asset| %>
<li class='grid_3'><%= image_tag(asset.photo.url, size: '100x100') %></li>
<% end %>
and the photos display perfectly. when I click on the edit page:
<%= f.simple_fields_for :assets do |asset| %>
<%= render :partial => 'asset_fields', :locals => { :f => asset } %>
<% end %>
the partial is rendered:
<div class='nested-fields'>
<%= f.file_field :photo %>
<% unless f.object.new_record? %>
<%= f.file_field :photo %>
<% #address.assets.each do |asset| %>
<li class='grid_3'><%= image_tag(asset.photo.url, size: '100x100') %></li>
<% end %>
<% end %>
</div>
at this point the photos each show up 3 times along with an extra image tag with a 'missing' photo placeholder. I would say I've tried different things but I just don't see what is wrong with the code. Any ideas?
Assuming simple_fields_for behaves the same as fields_for... the call to f.simple_fields_for :assets is a loop already. It loops over f.object.assets and renders the contents of the block once for each item found using the asset form generator (it may be a little less confusing to call that block variable asset_form, by the way). So since you're then rendering a partial that also renders all of the assets for the #address that's why you get each image 3 times.
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