Im made a gallery in rails by creating a div for each image-url in the database in my view. I would like to add a feature, that the user can specify the pictures per row and the number of rows per page on my edit-page. The value will be written to the database and the pagination should than be dynamic using the calculated value (pictures_per_row * rows_per_page).
Problem: Im using images.each method in my view to loop through the pictures and create a div for every image in the gallery. So they are created one after another. Concerning the results are paginated, gives me a specific number for pictures per page. But how do I accomplish something like:
for gallery.image.each do a div, but after 5 divs, continue in new line.
I think the pagination isnt the problem here, because it will paginate whatever the database query delivers.
Any ideas? Thanks in advance.
You could use each_slice. Something like:
<% #gallery.each_slice(5) do |slice| %>
<div class="row">
<% slice.each do |item| %>
<!-- Display images-->
<% end %>
</div>
<% end %>
Related
I have a table called Person with roughly a million records. I would like to display each of them as a card on the index page, but I am having performance issues. The view is rendered very slowly and is causing significant lag.
I simply had #persons = Person.all but it didn't work since it just crashed my app. Now, I am using the Person.find_in_batches method, which now allows me to display all the records within the page. Not only it takes a long time to load up the records but when I try to click a few buttons on the page, it either freezes or lags heavily. I'm lost on how I can improve performance.
For the record, here is my following code:
Person controller:
def index
batch_size = 5000
#people_in_batches = Person.find_in_batches(batch_size: batch_size)
end
Index view:
<% #people_in_batches.each do |batch|%>
<% batch.each do |person| %>
<div>
<span><%= person.name %></span>
<span><%= person.date_if_birth.strftime("%m/%d/%Y") %></span>
</div>
<% end %>
<% end %>
Thank you!
It doesn't matter how you query the million records, it still a million records.
What you should do is consider adding pagination on your page, so you would only needs to fetch records for the corresponding page. Just like Gmail.
So on the backend, you can use gem like https://github.com/kaminari/kaminari to help you with pagination.
Other than that, you should also consider adding filters on the page too, e.g. Active / Not Active, to reduce the number of records on page load.
I have with me a set of posts created from a scaffold and images uploaded using paperclip gem. I would like to show on each users profile the last six images from the users last posts and for that I am filtering as such
<% #posts.limit(6).each do |tweet| %>
<% if post.media.present? %>
<%= link_to image_tag(post.media.url(:thumb)), tweet %><br>
<% end %>
<% end %>
In my controller I have the posts arranged as such
#posts = user.posts.order('created_at desc')
My question is. Why is it that when using the above code, NO image is displayed. On upping the count to 9, I get three images displayed, to 12 i get six images displayed and so on
First of all, you should not make the calls to the db/model (with limit) in the views. Call it directly in the controller :
#posts = user.posts.order(created_at: :desc).limit(6)
Then, I would do some debugging to be sure you have the records you want.
Use byebug for instance and call it just after you set you #posts instance variable to see what it contains. Do all the posts contain a media ?
I have a twitter like app developed in rails 3.2.1. In my index page, I want to show the tweets posted by me and from the people whom I follow. Its working properly. Now what I need is to avoid the fetching of all the tweets. At first, I may need only the first 15 tweets then as I scrolls down, next batch of 15 or 20 tweets must append at the bottom just like what facebook and twitter does.
My index action looks like this:
def index
#tweets=Tweet.find(:all,:include =>[:user])
end
My view part is now fetching all the tweets and it looks like this:
<% #tweets.each do |tweet| %>
<% if tweet.user==current_user || current_user.following?(tweet.user) %>
<div class="marginer">
<%= tweet.user.user_name %> : <%= tweet.message %>
</div>
<% end %>
<% end %>
Please help
You can achieve this by means of AJAX calls to your server with the user details, batch size and the start index of the batch.
you can use lots of jquery plugin to achieve this ..like
http://jscroll.com/
and
http://www.jquery4u.com/tutorials/jquery-infinite-scrolling-demos/
I'm attempting to create a loop which shows stars as reviews are placed for a product, i have it so that it shows the rating as a number however i wish for it to display an image relating to the rating number,
i've currently got
<%=product.no_of_stars.to_i do image_tag "star-on.png" end %>
however it just displays the rating figure and not the number, no doubt I've missed something simple.
I've researched other questions and they state that should be enough for what i want, but of course its not working as expected.
Thanks, Ben.
The above answer is not quite correct. The problem is that Integer#times returns the integer it was called on, so you will still get 5 as the result. Try
<% product.no_of_starts.to_i.times do %>
<%= image_tag "star-on.png" %>
<% end %>
Try this.
<%=product.no_of_stars.to_i.times do image_tag "star-on.png" end %>
You are missing the times method. This is what allows you to run the number as a loop over and over again (super simplification).
I have an object called #events containing about 50 records being pulled from a find condition of my model.
I'm currently displaying the results of the #object in my view like this....
<% for event in #events %>
<p><%= #event.name %></p>
<% end %>
Instead of displaying the entire 50 I would like shrink the set to about 10 records so it displays better on the page.
I cannot use :limit in the find condition since the object is being composed from a variety of loops where after each iteration it adds a few specific records.
So the issue is I have this object #events with 50 records, how can I change the object after its been composed so only the first 10 records remain?
First of all, if you'd like to have pagination, I strongly suggest taking a look at will_paginate
Alternatively, you can do the following to read the first 10 records only.
<% #events.first(10).each do |event| %>
<p><%= event.name %></p>
<% end %>
Or the last 10 records
<% #events.last(10).each do |event| %>
<p><%= event.name %></p>
<% end %>
I didn't test it but you get the point.
are you looking to completely do away with the other 40 or are you just wanting to pull off 10 per page for display purposes. if you are just doing this for display purposes i would look into the will_paginate gem. through its options you could set it so only 10 results per page are shown.
Take a look at will_paginate and kaminari. They both are designed to limit the records retrieved from the database, plus offer helpers for your views to provide the usual number of pages and current page lists.
Will_paginate has been around a while, and is pretty flexible. Kaminari is newer and looks like it has a cleaner interface.