Handling .Erb rails with Nils

keeps giving me grief when the profile is nil ... what can i do?

+5
source share
3 answers

That should work too

<%= image_tag(this.profile.expiring_url(180)) rescue "no image!" %>
+6
source

Always check to see if there is a variable nilbefore using it in a view.

<% image_tag this.profile.expiring_url(180) unless this.profile.nil? %>

I am sure there is a more elegant solution to the problem, but this should get you started.

+7
source

, , . ? ?

, , :

def expiration_url_for( profile )
  (profile && profile.expiring_url(180)) || default_url
end

:

<%=image_tag expiration_url_for( this.profile )%>
+2

All Articles