Rails Carrierwave url is nil unless queried again - ruby-on-rails

I am having an issue with carrierwave/active-record in my rails application. My problem is that the following code resolves all of the images to null
<% Ad.all.limit(30).each do |ad| %>
<img src="<%= ad.carrier_image.url %>" >
<% end %>
Where as the following renders all the images just fine
<% Ad.all.limit(30).each do |ad| %>
<img src="<%= Ad.find(ad.id).carrier_image.url %>" >
<% end %>
The urls are there, just on the initial loop the carrier_image does not seem to be preloaded into the active record objects I think this is an issue with my understanding of rails eager loading, but I am having trouble figuring out how one would avoid this issue

What happens when you use the rails image_tag helper? You're also missing the brackets off the url call as per the carrierwave documentation, but I don't think that's what's causing you drama.
<%= image_tag ad.carrier_image_url() %>
As a side note, you should really move the "Ad.all.limit(30)" out of your view if you wish to keep it all "railsy".

Related

Can not move to page when I click the image (using gem 'rinku')

I'm using Gem:Rinku and trying to combine an image with a link that someone add from browser.
But there is no image on the browser when I write below code and just url link on the page.
show.html.erb
<div class="posted_share_item_link">
<%= Rinku.auto_link(#item.item_link, :all, 'target="_blank"').html_safe do %>
<%= image_tag "amazon_logo.jpg", class:"amazon_image" %>
<% end %>
</div>
I thought it works if I use do-end and put image_tag in it but actually it doesn't.
so, that would be great if someone knows how I can fix it.
Thank you.
the link is not active
Here's the fixed version.
<div class="posted_share_item_link"><%= Rinku.auto_link(#item.item_link, :all, 'target="_blank"') do %>
<%= image_tag "amazon_logo.jpg", class:"amazon_image" %>
<% end %>.html_safe
You should call html_safe on end of chain instead of middle. In your version, you passed block to result of html_safe which is string and that's why it didn't work

Rails 5 each loop activestorage files displays files attributes

I updated Rails to 5.2 version with ActiveStorage
Everything works fine, but when I perform a loop through my model attached files in order to display them as bootstrap cards, I see my files attributes on the page.
I don't want it to display anything.
How can I prevent that?
<strong style="margin: 10px">Files :</strong>
<div class="card-columns">
<%= #mymodel.files.each do |file| %>
<% end %>
</div>
what it makes on my page
Remove the = from your loop header...
Instead of
<%= #mymodel.files.each do |file| %>
Use
<% #mymodel.files.each do |file| %>
Checkout what the difference is

Rails each skipping if nil value

I'm currently iterating through a list of arrays, referencing data from a joined record (Students):
index.html.erb
<% #activities.compact.each do |activity| %>
<div class="img" style="background-image: url(<%= activity.student.image.url(:thumb) %>)">
<% end %>
If for some reason a user deletes the joined record (Student), Rails throws this error:
NoMethodError in Activities#index
undefined method `image' for nil:NilClass
Is there a way to tell Rails to still render the page but sub in a default value to that record if none exists?
Sure! For example,
activity.student.try{|s| s.image.url(:thumb)} || image_path("default.png")
You might want to put that into a helper though, since it seems like it's starting to get complicated.
Also, I would be wary of XSS problems here if users control any part of the attachment URL (e.g. if you are using the filename of their uploaded file). I'm not sure whether erb does the right thing when outputting to an attribute. Maybe it does---I'm just not sure.
If you use carrierwave gem for uploads, there'a built-in solution for default images https://github.com/carrierwaveuploader/carrierwave#providing-a-default-url
Try
<% #activities.compact.each do |activity| %>
<% if activity.student.present? %>
<div class="img" style="background-image: url(<%= activity.student.image.url(:thumb) %>)">
<% end %>
<% end %>

image_tag not working for all images the same way

I have a rails app (following Agile Web Development with Rails 4 -book) I'm trying to get hosted on apache+passenger+mysql -configuration. Now I've gotten most of the stuff working, but some of the images are not working correctly. For some images, image_tag "logo.jpg" for example the output in the html is correctly <img alt="Logo" src="/assets/logo-0f0773ff0a4264e6952c45931e50f722.png" />.
However, for another image (located in the very same folder), the html that comes out looks like this <img alt="Cs" src="/images/cs.jpg" />. This comes from a loop like this:
<% #products.each do |product| %>
<%= image_tag(product.image_url, class: 'list_image') %>
<% end %>
The image_url of the first product is cs.jpg. To make things even more curious, if I run rails console and run this command
pry(main)> ApplicationController.helpers.image_tag Product.first.image_url, class: 'list_image', I actually get the expected output of
=> "<img alt=\"Cs\" class=\"list_image\" src=\"/assets/cs-e1864c1128d6c90247bcd9275228284b.jpg\" />"
So how is it possible that the same helper method gives me different results with the (at least seemingly) same invocation?

Page not working on Heroku

I made an app in RoR and it's working perfectly locally, but there's only one page that's not working when I upload to Heroku. It's a view that has only this in the code:
<h1>Listagem</h1>
<% #items.each do |item| %>
<%= link_to item.materia, public_path(item) %>
<% end %>
<br />
I noticed that if I remove the public_path(item) the page works, so, is there any alternative to this?
THe only thing that sticks out to me is that you may have misspelled material in
<%= link_to item.materia, public_path(item) %>
this would throw an exception. You might want to check your logs as well

Resources