Search code examples
ruby-on-railslightbox2

lightbox doesn't want to open image from folder


I'm trying to setup lightbox plugin to load images from folder, but it don't want to open image. When I click on image and it trying open with lightbox, in terminal I get Routing error, no route matches. Lightbox is working, tried with external image link, but it not working with images from folder.

This is what I have so far.

index.html.erb

<div class="container">
  <id class="gallery">
    <div class="row">
      <% @images.each do |image| %>
        <div class="col-md-2">
          <a href="<%= image %>" class="img-responsive" data-lightbox="my-images">
            <%= image_tag image, class: "img-responsive" %>
          </a>
        </div>
      <% end %>
    </div>
  </id>
</div>

controller.rb

  def gallery
    @images = Dir.chdir(Rails.root.join('app/assets/images')) do
      Dir.glob("galleria/landscape/*.jpg")
    end
  end

application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require lightbox
//= require_tree .

application.css

 @import "lightbox";

Source code:

<a href="galleria/portrait/17-min.jpg" class="photo-hover img-responsive" data-lightbox="my-images"">
    <img class="img-responsive" src="/assets/galleria/portrait/17-min-ec4c03d557eb21edfdb115aaf9a69eab86e27b247385b8af3bb88a87cb398fd5.jpg" alt="17 min" />
</a>

Solution

  • Correct example

    Problem was with a href url.

    <% @images.each do |image| %>
      <div class="col-md-3 portrait">
        <a href='<%= "/images/#{image}" %>' data-lightbox="my-images">
          <%= image_tag image, class: "img-responsive" %>
        </a>
      </div>
    <% end %>