Rails lightbox2 ActionController::RoutingError - ruby-on-rails

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.

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.

I've never used lightbox2 before
but if it's me, I would take 3 different solutions
change "a" into "div"
remove href from "a"
overwrite event handler of lightbox to preventDefault
hope any of those can solve your problem

Related

Is there a conflict with this code and FancyBox3?

Can anyone tell me whats wrong with this section of code? I recently got this to work, but found out soon after the functionality for the Fancybox gallery stopped working. The a link element is only a sliver compared to the over all element. Im not sure if thats it. I know the most recent edit I made was to fix the surrounding row from .row-fluid to .row since that broke my layout. Below is the code:
<body id="portfolio">
<div class="container-fluid" id="particles-js"></div>
<%= render 'layouts/altmenu_gallery' %>
<h1>Portfolio</h1>
<div id="gallery" class="container-fluid">
<% #photos.each_slice(4) do |group| %>
<div class="row ">
<% group.compact.each do |photo| %>
<div class= "col-md-3">
<a class="fancybox" data-fancybox="gallery" href="<%=image_path photo.file_url %>" data-caption="<%= photo.description %>">
<%= image_tag photo.file_url, class:' img-fluid img-thumbnail' if photo.file.present? %>
</a>
</div>
<% end %>
</div>
<% end %>
<br class="clear">
</div>
<%= link_to 'New Photo', new_photo_path %>
</body>
Sorry, but it is not possible to tell without seeing the actual html code or, preferably, live demo. Maybe this issue arises because you have not escaped photo.description and that breaks html code, but, as I said, I can not be sure.

How can I call a link_to helper method in a block, linking to an image file while using a class as well?

This code is one element of a gallery. The "a href" tag is crucial for making it possible to expand an image by clicking on it. It is important that I have "a href" otherwise the javascript plugin (magnific-popup) won't work.
<a href="app/assets/images/1-fullsize.jpg" class="portfolio-box">
<img src="app/assets/images/1-thumbnail.jpg" class="img-responsive" alt="">
<div class="portfolio-box-caption">
<div class="portfolio-box-caption-content">
<div class="project-category text-faded">
Category
</div>
<div class="project-name">
Project Name
</div>
</div>
</div>
</a>
So far I came up with this:
<a href="app/assets/images/1-fullsize.jpg" class="portfolio-box">
<%= image_tag("1-thumbnail.jpg", alt: "", :class => "img-responsive") %>
<div class="portfolio-box-caption">
<div class="portfolio-box-caption-content">
<div class="project-category text-faded">
Category
</div>
<div class="project-name">
Project Name
</div>
</div>
</div>
</a>
So the images are visible but the the expanding of the picture is obviously not working in Rails. I thought about implementing an link_to helper method inside a block. But I have no idea how I can use the link_to helper method, link it to a file and additionally giving it a class inside a block? Is there a different solution? Does someone have any hints? If you need further information just let me know?
How can I call a link_to helper method in a block, linking to an image file while using a class as well?
You can use the the image_path as second option of link_to.
In Example:
<%= link_to(image_path("1-fullsize.jpg"), class: "portfolio-box") do %>
<%= image_tag("1-thumbnail.jpg", alt: "", :class => "img-responsive") %>
<div class="portfolio-box-caption">
<div class="portfolio-box-caption-content">
<div class="project-category text-faded">
Category
</div>
<div class="project-name">
Project Name
</div>
</div>
</div>
<% end %>
http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_path
http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Bootstrap does not work as it should on my Rails App

I'm trying to create a two column layout. The first side should span 9 columns and the next one 3. Inside the col-md-9 I want to nest two more columns, one that spans 4 and another one that spans 8.
<div class="row">
<% #huddles.each do |huddle| %>
<div class="col-md-9">
<div class="row">
<div class="col-md-4">
<img class="img-responsive" src="http://placehold.it/300x150">
</div>
<div class="col-md-8">
<h4><%= huddle.title %></h4>
<h4 class="huddle-description"><%= huddle.description %></h4>
<%= link_to "Read More...", huddle_path(huddle) %>
</div>
</div>
<% end %>
</div>
<div class="col-md-3">Second Column</div>
</div>
This however, comes out looking like this:
Am I nesting my rows and columns wrong? Or maybe it is my ruby code itself that screws up the layout once new "Huddles" are created?
EDIT: With the fixed code, the second column "col-md-3" comes out next to the last created huddle. Inspecting it, all the huddles make one single row.
<div class="row">
<% #huddles.each do |huddle| %>
<div class="col-md-9">
<div class="row">
<div class="col-md-4">
<img class="img-responsive" src="http://placehold.it/300x150">
</div>
<div class="col-md-8">
<h4><%= huddle.title %></h4>
<h4 class="huddle-description"><%= huddle.description %></h4>
<%= link_to "Read More...", huddle_path(huddle) %>
</div>
</div>
</div>
<% end %>
<div class="col-md-3">Second Column</div>
</div>
And looks like this, where the second column moves all the way down next to the last huddle created:
I think this is what you are going for. I am pretty confident the issue is with the html structure and has nothing to do with ruby.
I also want to clarify. The initial issue you were having was that you were starting a div inside the loop but only closing it outside the loop. So starting n amount of divs but only one closing tag, and that would cause it to look like the image you first posted.
<div class="row">
<div class="col-md-9">
<% #huddles.each do |huddle| %>
<div class="show-region" >
<div class="col-md-4" style="height:150px;">
<img class="img-responsive" src="http://placehold.it/300x150">
</div>
<div class="col-md-8" style="height:150px;">
<h4><%= huddle.title %></h4>
<h4 class="huddle-description"><%= huddle.description %></h4>
<%= link_to "Read More...", subjects_path(huddle) %>
</div>
</div>
<% end %>
</div>
<div class="col-md-3">Second Column</div>
</div>

facebook comments plugin to show comments dynamically

i use facebook comments plugin https://developers.facebook.com/docs/plugins/comments/
for every page in my site
and i show the facebook comments by fetching from json request
https://graph.facebook.com/comments/?ids=abc.com/
so when i update comment in facebook plugin, i would like to get the comments in my site to get dynamically updated
my code in controller
#fb_res=HTTParty.get('https://graph.facebook.com/comments/?ids=abc.com/')
fb_json=JSON.parse(#fb_res)
#fb_comment = fb_json["abc.com/"]["comments"]["data"].map {|x| [x["from"]["id"],x["message"],x["from"]["name"]]}
my views :
<% #fb_comment.each do |fb| %>
<li>
<div class="pic_commnt">
<div class="pic">
<div class="pic_image">
<img src="http://graph.facebook.com/<%= fb[0].to_s %>/picture?type=square" alt="" />
</div>
<span class="pic_name"><%= fb[2].to_s %></span>
</div>
<div class="pic_comments">
<p>
"<%= fb[1].to_s %>"
</p>
</div>
</div>
</li>
<% end %>
i would like to get this section updated dynamically

Each_with_index not working properly

I m using carousel to display the images. I ve uploaded the images in the cloud. But somehow it is repeating it self.
Here is my code
div id="myCarousel" class="carousel slide">
<div class="carousell-inner">
<% #img.each_with_index do |i|%>
<% if i.gallery == "Art"%>
<div class="active item">
<%= image_tag (i.image.url) %>
</div>
<% i.next%> <!-- For incrementing the i value but it is not working-->
<div class="item">
<%= image_tag (i.image.url)%>
</div>
<%end%>
<%end%>
</div>
</div>
What i want is in the active item it should display the first url of the image array and in the inactive item (which ll be active afterwards) should have the other images. It should not repeat the images. Here it is repeating the image since the value of i is not changing.
I am starting to understand what you want :)
You can make your logic very simple, if you will separate your data.
<div id="myCarousel" class="carousel slide">
<div class="carousell-inner">
<% #img.keep_if{|i| i.gallery == "Art"}.each do |i|%>
<div class="active item">
<%= image_tag (i.image.url) %>
</div>
<%end%>
<% #img.keep_if{|i| i.gallery != "Art"}.each do |i|%>
<div class="item">
<%= image_tag (i.image.url)%>
</div>
<%end%>
</div>
</div>
And better to prepare your data in controller, not in view.
I first thought you needed each_with_index, but actually, you are only interested in images from the Art gallery, no telling if the first image is actually an Art image, so I tried as follows:
<div id="myCarousel" class="carousel slide">
<div class="carousell-inner">
<% art_images_counter = 0 %>
<% #img.each do |image|%>
<% if image.gallery == "Art" %>
<% if art_images_counter == 0 %>
<div class="active item">
<%= image_tag (image.image.url) %>
</div>
<% else %>
<div class="item">
<%= image_tag (image.image.url)%>
</div>
<% end %>
<% art_images_counter += 1 %>
<%end%>
<%end%>
</div>
</div>
Hope this helps.
ive found out the answer by doing it manually. First of all in the controller i ve found out all the images which belong to the Art Gallery by this code.
#image = Image.find_all_by_gallery("Art")
Now to display the first element of the array #image, we can use the simply #image.first and then we can call that first element of the array in the active item of carousel. Then for the item part the basic thing to understand is that if we directly call and display all the elements of the array then the first element of the array will also be display in this part. So the repetition can happen. To avoid this we can do like this:
i = #image.count
#image[1..i]
So that the first element will not repeat in the `item' part of the carousel.
Hope it might help others. Thanks for giving time.

Resources