Image Gallery in Rails - ruby-on-rails

I have a multiple upload images, using carrier wave, on my application and with the help of you guys, in another question, I was able to display all the images with a each method. It works very well but I would like to improve it. I want to do a image gallery with a carousel and when the user click at it opens a modal with the image bigger.
I try to do following the tutorial of W3C School but just doesn't work.
When I try to put the Onclick method the page doesn't render.
https://www.w3schools.com/howto/howto_js_lightbox.asp
There is any gem that help me do this? There is any tutorial for a rails application?
I am displaying the images with the following code:
<% #imovel.imagens.each do |imagem| %>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<%= image_tag(imagem.url) if #imovel.imagens? %>
</div>
</div>
<% end %>

You will use link_to to create the a tag with a block. The arguments are:
link_to(url, html_options = {})
Inside the block, you will put the html code inside the a tag (your image).
<% #imovel.imagens.each do |imagem| %>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<%= link_to imagem.url, class: 'your-css-class', 'data-toggle': 'lightbox', 'data-gallery': 'name-of-gallery' do %>
<%= image_tag(imagem.url) %>
<% end if #imovel.imagens? %>
</div>
</div>
<% end %>

Related

Error when try to display photos on homepage : "The asset "courses/image1" is not present in the asset pipeline"

I am newbie with Rails and trying to display photos to my homepage and here is my code at my home.html.erb
<br/>
<div class="row">
<% #courses.each do |course| %>
<div class="col m4">
<div class="card">
<div class="card-image">
<%= link_to course do %>
<%= image_tag "courses/#{course.image}" %>
<% end %>
</div>
<div class="card-content">
<span class="card-title"><%= course.title %></span>
<% (1..course.star).each do %>
<1 class="material-icons" green-text>grade</i>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
I made another page named "course" to upload photo to the homepage.
Then, when I ran the code, it said to me that at the line <%= image_tag "courses/#{course.image}" %> there was an error. The asset "courses/image1" is not present in the asset pipeline.
Here is what I did.
First, I went to app/assets/images.
Second, I made folder which named "photos".
Third put image to the folder "photos".
And Last I tried the code but there was the error.
Could you please give me some ideas? Thank you very much.
Rails is pretty smart, so when you provide it a path in an image_tag, such as courses/images1, it prepends this path with app/assets/images (by default) such that the full relative path is app/assets/images/courses/images1.
So to update your image_tag you should firstly pass it your image file type: .jpg or .png etc. Second update your path to photos/images.file_type where filetype is your image filetype. If you want to put a /courses directory in your /photos file, and place image1.file_type in there, then your path would be photos/courses/images.file_type.

I have called an image from Active Storage with url_for. Is there any way, to link that image with the post that belong to? in rails

i have this call for the image from Active Storage, using url_for
<% #posts.each do |post| %>
<div class="card-group card-group-size">
<div class="card recipes recipes_posts">
<img src="<%= url_for(post.image) if post.image.attached? %> " class="index_images">
<div class="card-body">
<h5 class="card-title"><%=link_to post.title, post, class: "post_title"%></h5>
</div>
</div>
</div>
<% end %>
This was the only way to call it there (/post/index.html.erb).
I could call the same image with <%= image_tag #post.image, class: "image_view" %> in posts/show.html.erb, but this wont work on any other page.
i dont have any issues with the loading of the image, that work as is should, i only wish to know if i can make onclick that image to link to the post that belongs.
thank you
Wrap your image in link tag passing link to show action. I assume there is a routes configure and available as helper post_path
<%= link_to post_path(post) do %>
<%= image_tag post.image, class="index_images" if post.image.attached? %>
<% end %>

link_to rails_blob_path when clicked does not match the same image of image_tag inside the swiper slide div

In my view when i click the link_to (the slides i see side by side), does not match the slide. I am using active storage each home has many attached home_photos. This is ok. But my issue is i don't know how to implement this on my swiper slider. My images are showing on my slide side by side perfectly, but when i click a slide, it's supposed to pop-out, and it does, but it only shows one image, the first one from home_photos[0]. Please help.
<div class="swiper-wrapper">
<!-- Slides-->
<% #home.home_photos.each do |photo| %>
<div class="swiper-slide">
<%= link_to rails_blob_path(#home.home_photos[0]), "data-toggle" => "gallery-top" do %>
<%= image_tag photo, class: 'img-fluid' %>
<% end %>
</div>
<% end %>
</div>
<div class="swiper-pagination swiper-pagination-white"></div>
<div class="swiper-button-prev swiper-button-white"></div>
<div class="swiper-button-next swiper-button-white"></div>
I think you have to change #home.home_photos[0] to photo in link_to:
<%= link_to rails_blob_path(photo), "data-toggle" => "gallery-top" do %>

Limiting photos with `#photos.last(5)` isn't working

This code doesn't throw any errors, but no photos appear. The <div> around the code is empty when I check in inspect element. If I change #user.photos.last(5) to #user.photos.each, the photos appears. But I want to show only 5 of the most recently added photos.
<%- #user.photos.last(5) do |photo| %>
<div class="col-xs-6 col-md-3 no-padding">
<a href="<%= user_photo_path(#user.username, photo.id) %>" class="photo-thumbnail">
<%= image_tag photo.url %>
</a>
</div>
<% end %>
What am I doing wrong?
Please take some time to go though documentation once again. it is just a minor problem.
<%- #user.photos.last(5).each do ....

Rails - conditional display of html block

On my views I use 1 form that includes a block that renders comments. I do not want to run it when creating a new record. So, I tried conditions like so...
<% unless #annotation_id.nil? %>
<hr>
<div class="row">
<div class="col-md-8">
<h4>Comments</h4>
<%= render #annotation.comments %>
</div>
<div class="col-md-4">
<%= render 'comments/form' %>
</div>
</div>
<% end %>
This however results in never displaying the block - also when the annotation record exists. What am I doing wrong?
You don't show that you have actually set #annotation_id to something.
A simpler way might be to use the .new_record? method instead, like:
<% unless #annotation.new_record? %>
...
<% end %>
use if #annotation.persisted? or unless #annotation.new_record?

Resources