Search code examples
ruby-on-railsrubylightboxlightbox2

Rails lightbox2 ActionController::RoutingError


I'm trying to implement lightbox2 into a rather simple rails 5 app and seem to be getting the following error:

ActionController::RoutingError (No route matches [GET] "/images/lightbox/bpo.jpg"):

Ive been following steps from: https://lokeshdhakar.com/projects/lightbox2/ & https://github.com/gavinkflam/lightbox2-rails

All images for the Lightbox are located within "images/lightbox" folder & images are properly displayed on the page however upon clicking on an image to enlarge and bring up the Lightbox modal the above routing error is displayed in the logs & no image appears.

gallery_controller.rb

def index
 @images = Dir.chdir(Rails.root.join('app/assets/images')) do
    Dir.glob('lightbox/*.jpg')
    end
  end

index.html.erb

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

The following has been added to my application.html.erb file <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> just before the closing body tag.

Any help is greatly appreciated as I'm confused on how to proceed.


Solution

  • Jus try this code snippet.

    <div class="jumbotron jumbotron-fluid">
      <div class="container">
        <div class="row">
            <% @images.each do |image| %>
              <div class="col-md-3">
                <a href='<%= image_path(image) %>' class="img-fluid" data-lightbox="my-images">
                  <%= image_tag image, class: "img-fluid" %>
                </a>
              </div>
            <% end %>
          </div>
      </div>
    </div>
    

    You need to use image_path helper method for href because assets are coming from through asset pipeline.