简体   繁体   中英

Rails: Cloudinary gem - default image not loading

I'm working on a pinterest-like app, using Cloudinary gem with Attachinary. I have added a line into my index.html.erb file which was supposed to display a default image if a created Pin didn't have one or the image didn't load properly.

 <% if pin.image.present?  %>
                  <%= link_to pin do %>
                  <%= cl_image_tag(pin.image.path) %>

            <% end %>    

             <% if pin.image.present? == false %>
                  <%= cl_image_tag('no-image-found_jqqruy') %>

                 <% end %>    

Unfortunately it doesn't work - the images for the pins having them display properly, but for ones who don't only a thick line is visible (jQuery Masonry grid). I'm wondering what is the proper way of setting a default image with cloudinary. Where did I go wrong? Here is my full index file:

<div id="pins" class="transitions-enabled">

       <% @pins.each do |pin| %>
          <div class="container">
          <div class="box panel panel-default">

             <% if pin.image.present?  %>
                  <%= link_to pin do %>
                  <%= cl_image_tag(pin.image.path) %>

            <% end %>    

             <% if pin.image.present? == false %>
                  <%= cl_image_tag('no-image-found_jqqruy') %>


             <% end %>    


            <div class="panel-body">

            <%= pin.description %> <br>
            <strong><%= pin.user.email if pin.user %></strong>

            <% if pin.user == current_user && user_signed_in? %>
                <div class="actions">
                  <%= link_to edit_pin_path(pin) do %>
                    <span class="glyphicon glyphicon-edit black"></span>
                      Edit
                         <% end %>

                  <%= link_to pin, method: :delete, data: { confirm: 'Are you sure?' } do %>
                     <span class="glyphicon glyphicon-trash black"></span>
                      Delete


                            </div>    

                         <% end %>     
                       <% end %>
                      </div>     
                  <% end %> 
                </div>  
            <% end %>            
          </div>

I will only add that everything worked fine until I've introduced the glyphicons.

I will be greatful for your help!

If there was no copy&paste error, there is an extra end missing to terminate the link_to pin do statement. So the first if statement is still active, and then an opposite if statement is queried, which of course will never be true.

I suggest to use if-else-end.

<% if pin.image.present?  %>
  <%= link_to pin do %>
    <%= cl_image_tag(pin.image.path) %>
  <% end %>    
<% else %>
  <%= cl_image_tag('no-image-found_jqqruy') %>
<% end %>    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM