I am using CarrierWave and Amazon S3 to upload images to my application, which is working correctly. But I also want to have the image link to the corresponding Movie. I have tried the following but it doesn't work. I have looked but can't seem to find a solution this simple problem.
<% if #movie.avatar? %>
<%= image_tag(#movie.avatar_url), link_to #movie %>
<% else %>
<img src="http://placehold.it/150x200">
<% end %>
Try this:
<%= link_to image_tag(#movie.avatar_url), #movie %>
Use this
<%= link_to (image_tag(#movie.avatar_url)), #movie %>
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 %>
In the index page of my app, i want to display only the first image of a post that has multiple images attached to it.
<% (0...job.images.count).each do |image| %>
<%= image_tag(job.images[image]) %>
<% end %>
I have tried:
<%= image_tag job.images.first.try[image] %>
But get a no method given error
You should be able to treat it as a normal ActiveRecord query
<%= image_tag job.images.first if job.images.attached? %>
Based on the first bit of code you've provided, you should be able to use this:
<%= image_tag job.images.first %>
Assuming that job.images is an array of paths to the images?
I'm having some issues with my project I'm using the image_tag with my DB but every time I want to use it, is not taking the syntaxis this is the message thar I got The asset "ur" is not present in the asset pipeline., this is the code:
<% #articles.each do |article| %>
<%=link_to article_path(article), class:"thumb-link" do %>
<%= image_tag ("#{article.img}") %>
<% end %>
<% end %>
in my DB in the field "img" I got this URL "https://dummyimage.com/700x900/000/C82E0D.jpg"
any idea where do I have my mistake???
I will really appreciate if you can help me with thi issue
I check my code again I found my mistake in the image_tag I need to add the https like this:
<%= image_tag ("https://#{article.img}") %>
and it works
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