Apply a condition only on image which is nil Rails 4 -


i'm new rails code ugly..

i have loop give me 3 latest articles on index page.

i added condition "if there no image in article, give me image unsplash".

the issue : that's apply condition on 3 images if 2 of 3 images exists. want image nil unsplash image. how can ?

<% @articles.last(3).in_groups_of(3, false).each |group| %>     <div class="row">       <% group.each |article| %>         <% if article.image.exists? %>           <div class="col-xs-12 col-sm-4">             <div class="card">               <style media>                 .card { background: linear-gradient(-225deg, rgba(30,30,30,0.6) 30%, rgba(46,46,46,0.5) 80%), url("<%= article.image.url(:medium) %>"); }               </style>               <img src="<%= article.user.image.url(:thumb) %>" alt="" class="avatar-small card-user">               <div class="card-description">                 <a href="<%= articles_path(article.slug) %>"><h3><%= article.title %></h3></a>                 <p><%= truncate(article.subtitle, length: 45, escape: false) %></p>               </div>             </div>           </div>      <% else %>           <div class="col-xs-12 col-sm-4">             <div class="card">               <style media>                 .card { background: linear-gradient(-225deg, rgba(30,30,30,0.6) 30%, rgba(46,46,46,0.5) 80%), url("http://unsplash.it/1280/500/?random"); }               </style>               <img src="<%= article.user.image.url(:thumb) %>" alt="" class="avatar-small card-user">               <div class="card-description">                 <a href="<%= articles_path(article.slug) %>"><h3><%= article.title %></h3></a>                 <p><%= truncate(article.subtitle, length: 45, escape: false) %></p>               </div>             </div>           </div>       <% end %>     <% end %>   </div> <% end %> 

my controller :

class pagescontroller < applicationcontroller   def index     @articles = article.last(3)   end end 

thanks lot !

i find error.

it <style media> issue.

i :

<% @articles.last(3).in_groups_of(3, false).each |group| %>     <div class="row">       <% group.each |article| %>           <div class="col-xs-12 col-sm-4">               <a href="<%= articles_path(article.slug) %>"><div style="background: linear-gradient(-225deg, rgba(30,30,30,0.6) 30%, rgba(46,46,46,0.5) 80%), url('<%= article.image.url(:thumb) %>');" class="card">                 <img src="<%= article.user.image.url(:thumb) %>" alt="" class="avatar-small card-user">               <div class="card-description">                 <h3><%= article.title %></h3>                 <p><%= truncate(article.soustitre, length: 45, escape: false) %></p>                 </div>               </div>           </div></a>         <% end %>     </div>   <% end %> 

now 3 latest articles displayed on index page. add default image paperclip , withdrawed condition if else.


Comments