I am trying to call image tag in a model and return the image if it exists other wise return null like this :-
def medium_avatar_exists?
if self.avatar.present?
image_tag self.avatar.thumb_medium_url
else
image_tag "missing-avatar-medium.png"
end
end
when I call this method from the view :- current_user.medium_avatar_exist?
I get an error saying undefined method image_tag what could be the issue ?
You can't use helper methods in model image_tag
is an helper method and you are trying to use it in the model hence it gives an error.
Try following instead in your application_helper.rb
or some other helpr you want
def medium_avatar_exists?(user)
if user.avatar.present?
image_tag user.avatar.thumb_medium_url
else
image_tag "missing-avatar-medium.png"
end
end
OR Just
def medium_avatar_exists?(user)
image_tag (user.avatar.present? ? user.avatar.thumb_medium_url : "missing-avatar-medium.png")
end