I have this code-snippet in html erb.
For some objects the cover_image_url is empty, how do i modify this code block to use a default value ,when that property is null or empty?
<%@books.each do |book|%>
$('#bookContainer').append('<div class="conn"><p><img class="floatright" src="<%= h book.cover_image_url%>"><h3><%= h book.title%></h3><h3><%= h book.author%></h3></p></div>');
<% end %>
You could define a
cover_image_urlmethod on your book model that will return a default value if there is nothing set in the database (I am assuming that cover_image_url is a column in the book table). Something like this:This will return
"/my_default_link"if the attribute is not set, or the value of the attribute if it is set. See section 5.3 on The Rails 3 Way for more info on this stuff. Defining a default value for a model in the model layer may be a little cleaner than doing it in the view layer.