Looping images in bootstrap carousal rails - ruby-on-rails

I'm having some trouble trying to display multiple images attached to a job post using bootstrap carousal. Here's the original code without the caraousal template:
<% if #job.images.attached? %>
<% (0...#job.images.count).each do |image| %>
<%= image_tag(#job.images[image]) %>
<% end %>
<% else %>
<%= image_tag "missing.jpg" %>
<% end %>
Below is a bootstrap carousal example i used from w3 schools and it works fine when i tested it on my app.
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active"></li>
<li data-target="#demo" data-slide-to="1"></li>
<li data-target="#demo" data-slide-to="2"></li>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<div class="item active">
<img src="la.jpg" alt="Los Angeles">
</div>
<div class="item">
<img src="chicago.jpg" alt="Chicago">
</div>
<div class="item">
<img src="ny.jpg" alt="New York">
</div>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
So far i've tried :
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<% (0...#job.images.count).each do |image| %>
<li data-target="#demo" data-slide-to=#{image} class="active"></li>
<% end %>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<% (0...#job.images.count).each do |image| %>
<div class="item active">
<%= image_tag(#job.images[image]) %>
</div>
<% end %>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
The images are stacking up against each other, and when i click on the next slide, all the images disappear.I'm not sure exactly what to put in the data-slide classes, i'm suspecting that's the cause of the problem.
UPDATE
This code is working when the image slider automatically plays, but manually clicking on an image slider causes the entire carousal to disappear:
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<% #job.images.each_with_index do |image, index| %>
<li data-target="#demo" data-slide-to=#{index} <%= index == 0 ? 'class="active"' : '' %>></li>
<% end %>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<% #job.images.each_with_index do |image, index| %>
<div class="item <%= index == 0 ? 'active' : '' %>">
<%= image_tag image %>
</div>
<% end %>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>

Firstly, it looks like you're giving the active class to every image in the slideshow. Secondly, there are easier methods of iterating through your images.
Something like this should work:
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<% #job.images.each_with_index do |image, index| %>
<li data-target="#demo" data-slide-to=#{index} <%= index == 0 ? 'class="active"' : '' %>></li>
<% end %>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<% #job.images.each_with_index do |image, index| %>
<div class="item <%= index == 0 ? 'active' : '' %>">
<%= image_tag image %>
</div>
<% end %>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>

Related

Using carousel bootstrap whit ruby and rails

Im using carousel bootstrap and my images are in cloudinary.
I cant see the images, i can see the buttons next and previus, the text but not the imges.
every idea counts.
Thanks
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<% #portfolios_consultoria.each_with_index do |p, index| %>
<div class="carousel-item <%= index == 0 ? 'active' : '' %>">
<img class="d-block w-100 url('<%= cl_image_path p.photo.key unless p.photo.key.blank?%>')">
</div>
<h3><%= p.name %></h3>
<% end %>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>

Put a link on the right of a card - bootstap

I have a page (show route) that looks like this:
and i would like to position the link to "All Camgrounds" to the right of the card. I tried several things like adding classes like "text-right" or "justify-content-end", but it did not work.
I am using the framework Boostrap-5.
Any help would be much appreciated. Here is the code for the SHOW page :
<% layout('layouts/boilerplate') %>
<div class="row">
<div class="col-6 offset-3">
<div class="card">
<div class="card-header">
<h2>
<%= campground.title %>
</h2>
</div>
<img src="<%= campground.image %>" class="card-img-top" alt="...">
<div class="card-body">
<p class="card-text">
<%= campground.description %>
</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item text-muted">
<%= campground.location %>
</li>
<li class="list-group-item">
$ <%= campground.price %> /night
</li>
</ul>
<div class="card-body">
<a class="card-link btn btn-warning" href="/campgrounds/<%= campground._id %>/edit">Edit</a>
<form class="d-inline" action="/campgrounds/<%= campground._id %>?_method=DELETE" method="post">
<button class="btn btn-danger">Delete</button>
</form>
<a class="card-link d-inline text-right" href="/campgrounds">All
campgrounds</a>
</div>
</div>
</div>
</div>
There are many different ways to align right in Bootstrap 5.
In your case it's probably easiest to use float-end.
<div class="row">
<div class="col-6 offset-3">
<div class="card">
<div class="card-header">
<h2> Title </h2>
</div>
<img src="//via.placeholder.com/500" class="card-img-top" alt="...">
<div class="card-body">
<p class="card-text"> campground.description </p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item text-muted"> campground.location </li>
<li class="list-group-item"> $ 39/night </li>
</ul>
<div class="card-body">
<a class="card-link btn btn-warning" href="/campgrounds/<%= campground._id %>/edit">Edit</a>
<form class="d-inline" action="/campgrounds/<%= campground._id %>?_method=DELETE" method="post">
<button class="btn btn-danger">Delete</button>
</form>
<a class="card-link float-end" href="/campgrounds">All campgrounds</a>
</div>
</div>
</div>
</div>
https://codeply.com/p/S9PyKHZC9y
Also note, left and right have been replaced with start and end in Bootstrap 5.

Bootstrap carousel in rails 6

I'm trying to create a bootstrap carousel for the homepage of a blog that scrolls through the last three articles from the database. I got it working for a carousel that advances automatically but I wanted to have the next and previous buttons but when I try in that one it does not work. I can get the first which is the last article in the table to display but it will not advance.
I couldn't figure out out a better way to get the last three articles so here is ow I did it. I am definitely open to suggestions to make this better. My home action for the pages_controller is:
def home
#articles = Article.last(3).reverse
#art1 = #articles.pop
#art2 = #articles.pop
#art3 = #articles.pop
end
Now the carousel:
<div id="carouselExampleCaptions" class="carousel slide" data-bs-ride="carousel">
<ol class="carousel-indicators">
<li data-bs-target="#carouselExampleCaptions" data-bs-slide-to="0" class="active"></li>
<li data-bs-target="#carouselExampleCaptions" data-bs-slide-to="1"></li>
<li data-bs-target="#carouselExampleCaptions" data-bs-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<%= image_tag(#art3.image.url, width: '100%') %>
<div class="carousel-caption d-none d-md-block">
<h5><%= #art3.title %></h5>
</div>
</div>
<div class="carousel-item">
<%= image_tag(#art2.image.url, width: '100%') %>
<div class="carousel-caption d-none d-md-block">
<h5><%= #art2.title %></h5>
</div>
</div>
<div class="carousel-item">
<%= image_tag(#art1.image.url, width: '100%') %>
<div class="carousel-caption d-none d-md-block">
<h5><%= #art1.title %></h5>
</div>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleCaptions" role="button" data-bs-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="visually-hidden">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleCaptions" role="button" data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</a>
</div>
This works in the works in a version of the carousel that does not have buttons but advances automatically.

Rails 5.2: Bootstrap 4 tab panel not populating properly

I have the following code in my app:
<!-- Nav tabs -->
<div class="row">
<div class="col-md-3">
<ul class="nav md-pills pills-primary flex-column" role="tablist">
<% #users_with_apps = [] %>
<% AppForm.all.order("created_at DESC").each do |app| %>
<% unless #users_with_apps.include? User.find(app.user_id) %>
<% #users_with_apps << User.find(app.user_id) %>
<% end %>
<% end %>
<% #users_with_apps.each do |user| %>
<% apps = [] %>
<% AppForm.all.order("created_at DESC").each do |app| %>
<% if app.user_id == user.id %>
<% apps << AppForm.find(app.id) %>
<% end %>
<% end %>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#<% user.id %>-panel" role="tab">
Member #<%= fixed_digit_number(user.id) %> (<%= apps.count %>)
</a>
</li>
<% end %>
</ul>
</div>
<div class="col-md-9">
<!-- Tab panels -->
<div class="tab-content vertical">
<% #users_with_apps.each do |user| %>
<% apps = [] %>
<% AppForm.all.order("created_at DESC").each do |app| %>
<% if app.user_id == user.id %>
<% apps << AppForm.find(app.id) %>
<% end %>
<% end %>
<!-- Panel 1 -->
<div class="tab-pane fade" id="<% user.id %>-panel" role="tabpanel">
<% apps.each do |app| %>
<%= render partial: "app_forms/app_line", locals: {application: app} %>
<% end %>
</div>
<!-- Panel 1 -->
<% end %>
</div> <!-- tab content -->
</div> <!-- col -->
</div> <!-- row -->
<!-- Nav tabs -->
It renders to look like this:
<!-- Nav tabs -->
<div class="row">
<div class="col-md-3">
<ul class="nav md-pills pills-primary flex-column" role="tablist">
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#2-panel" role="tab">
Member #00002 (1)
</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#1-panel" role="tab">
Member #00001 (13)
</a>
</li>
</ul>
</div>
<div class="col-md-9">
<!-- Tab panels -->
<div class="tab-content vertical">
<!-- Panel 1 -->
<div class="tab-pane fade" id="2-panel" role="tabpanel">
...panel 2 content...
</div>
<!-- Panel 1 -->
<!-- Panel 1 -->
<div class="tab-pane fade" id="1-panel" role="tabpanel">
...panel 3 content...
</div>
<!-- Panel 1 -->
</div> <!-- tab content -->
</div> <!-- col -->
</div> <!-- row -->
<!-- Nav tabs -->
Ideally, this would create a list of users on the left, and (depending on which user was clicked) their applications would populate on the righthand side.
Unfortunately, no matter what user is clicked, it displays the applications of the first user on the list.
Can anyone see what logic is going wrong here?
The only problem which I am seeing here now is panel ids and link to that panel id shouldn't start with a number. Instead, you can use panel-<id>. i.e: panel-1.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<!-- Nav tabs -->
<div class="row">
<div class="col-md-3">
<ul class="nav md-pills pills-primary flex-column" role="tablist">
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#panel-2" role="tab">
Member #00002 (1)
</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#panel-1" role="tab">
Member #00001 (13)
</a>
</li>
</ul>
</div>
<div class="col-md-9">
<!-- Tab panels -->
<div class="tab-content vertical">
<!-- Panel 1 -->
<div class="tab-pane active" id="panel-2" role="tabpanel">
...panel 2 content...
</div>
<!-- Panel 1 -->
<!-- Panel 1 -->
<div class="tab-pane fade" id="panel-1" role="tabpanel">
...panel 3 content...
</div>
<!-- Panel 1 -->
</div> <!-- tab content -->
</div> <!-- col -->
</div> <!-- row -->
<!-- Nav tabs -->

Why is my ternary operator not working on a class in ERB to load images from a database to a bootstrap carousel?

I am trying to load images in a bootstrap carousel with images stored in a PostgreSQL database on my Ruby on rails app. I am using ERB on the front end. With the code below nothing shows up... I believe it has something to do with my ternary operator on my class but i am not sure what the exact problem is....
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<% #post.first(3).each do |image, index| %>
<li data-target="#myCarousel" data-slide-to="<%= index %>" class="<%= index == 0 ? 'active' : '' %>"></li>
<% end %>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<% #post.first(3).each do |image, index| %>
<div class="item <%= index == 0 ? 'active' : '' %>">
<%= link_to image_tag(image.image.url, class:"images") %>
<div class="">
<h3><%= index %></h3>
</div>
</div>
<% end %>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
Reason being your ternary operator are not getting index to make it working you need to do with each_with_index instead of each
So here is right way to do:
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<% #post.first(3).each_with_index do |image, index| %> <!--use each_with_index -->
<li data-target="#myCarousel" data-slide-to="<%= index %>" class="<%= index == 0 ? 'active' : '' %>"></li>
<% end %>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<% #post.first(3).each_with_index do |image, index| %> <!--use each_with_index -->
<div class="item <%= index == 0 ? 'active' : '' %>">
<%#= link_to image_tag(image.image.url, class:"images") %> <!--use image_tag -->
<%=image_tag image.image.url ,class: "images"%>
<div class="">
<h3><%= index %></h3>
</div>
</div>
<% end %>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
I have just fixed the issue, if images are there than it should work now. let me know for further guidance.

Resources