Reputation: 137
I have one model that is
class Retailer < ActiveRecord::Base
...
has_attached_file :onboarding_image
...
end
If the onboarding_image is not present, when doing retailer.onboarding_image_url
i'm getting: "/onboarding_images/original/missing.png"
Is there a config to get nil
when no image is present? i know that i can do something like
retailer.onboarding_image.present? ? retailer.onboarding_image : nil
but that is not the approach i want to follow, cause i have thousand of attached_files in all of my code.
Upvotes: 1
Views: 361
Reputation: 1226
if you have this attached files on multiple models, you can create concern for all classes that have has_attached_file
:
module BoardUrl
def board_url
onboarding_image.present? ? onboarding_image.url : nil
end
end
class Retailer
include BoardUrl
end
This way you can use your board_url
method on any model you need.
Upvotes: 0
Reputation: 7779
You can set the default_url
as outlined here:
has_attached_file :onboarding_image, default_url: ''
Another approach is to override the onboarding_image_url
method:
def onboarding_image_url(default_value=nil)
onboarding_image.present? ? onboarding_image.url : default_value
end
Upvotes: 1