Change the way we retrireve the images urls

For some reason the paperclip method `attachment.exists?' was
returning nil even when `attachment.url(style)' was correctly
returning the url/path of the attachment.

Therefore returning `nil' was causing to raise an error in the
method `image_tag'.

With this change we make sure we return the image url it it's
available, or an empty string if it's not, but never a null value.
This commit is contained in:
Julian Herrero
2019-04-04 16:40:26 +02:00
parent defbb25ec5
commit c9752e94be
2 changed files with 3 additions and 3 deletions

View File

@@ -6,7 +6,7 @@ module Galleryable
accepts_nested_attributes_for :images, allow_destroy: true, update_only: true
def image_url(style)
image.attachment.url(style) if image && image.attachment.exists?
image&.attachment&.url(style) || ""
end
end
end

View File

@@ -6,7 +6,7 @@ module Imageable
accepts_nested_attributes_for :image, allow_destroy: true, update_only: true
def image_url(style)
image.attachment.url(style) if image && image.attachment.exists?
image&.attachment&.url(style) || ""
end
end
end