I have been working on implementing dynamic background images for multiple divs in index.html.erb. Currently, I have achieved this by using inline styling with the background URL fetched from my database (using carrierwave). The code snippet looks something like this:
<div class="style-box col-8 ml-auto mr-auto" style="background: url(<%= place.photos.first.picture.url %>)">
<h1><%= link_to place.name, place_path(place) %></h1>
<i><%= place.address %></i>
<br /><br />
<p><%= place.description %></p>
The challenge I'm currently facing is that when a new place is created without an image, the view fails to render the complete HTML page due to incomplete data (nil value for place.photos). Is there a way, either using this method or a similar approach, to ensure that the background image only overrides the default style-box background if an image for a new place is present? Normally, I would use an if statement, but I'm struggling to figure out how to use an if statement to update CSS with Rails.