Paginate with Kaminari not working - ruby-on-rails

I am trying to use basic Kaminari to do some pagination, but I am having trouble getting it to work properly.
I have installed the Kaminari gem and then in my controller I have the following code:
def new
#guestbook = Guestbook.new
#guestbooks = Guestbook.all.limit(5).page(params[:page])
end
In my associated new view I have this code...
<%= paginate #guestbooks %>
<div class="span1">
<% #guestbooks.each do |g| %>
<br/>
<h4><%= g.name %>, <%= g.created_at %><br/></h4>
<%= g.message %><br/>......
However when I reload my page I do not see any pagination.

You need to use this in your controller:
#guestbooks = Guestbook.all
#guestbooks = Kaminari.paginate_array(#guestbooks).page(params[:page]).per(5)
And add this in your view:
<%=paginate #guestbooks%>

I completely support #Yang answer, and also I would suggest to make sure you have given min number of entries which is greater than the per page value.
for example:
def index
#posts = Post.page(params[:page]).per(10)
end
In this case: you must be created more than ten entries to get the pagination.

Related

Rails, duplicate models show on template

On my template, I show books ordered by their child's created_at (reviews). But when I order it this way, the books are shown twice/thrice (varies). How can I resolve this?
My controller:
#pagy, #books = pagy_countless(Book.where(user_id: current_user.id).joins(:comments).order("comments.created_at DESC").group("books.id, comments.created_at"), items:10 )
# I am using pagy gem
As you can see, I've tried group(). Doesn't help. I've also tried adding .distinct to the end of the code (and in the middle). Doesn't help.
#pagy, #books = pagy_countless(Book.where(user_id: current_user.id).joins(:comments).order("comments.created_at DESC").distinct, items:10 )
I would suggest not using the pagy gem while you're debugging this. Add it back later when you've got a non-paged version working.
The joins will indeed produce multiple instances of Book. What you need is
#books = Book.where(user_id: current_user.id).includes(:comments)
Then in the template (you didn't show us, so I'm making assumptions":
<%- #books.each do |book| %>
<h2><%= book.title %></h2>
<ul>
<%- book.comments.each do |comment| %>
<li><%= comment.text %></li>
<%- end %>
</ul>
<%= end %>
(hope I got all the erb right there! I've been using haml for years!)
you could use select MAX each group instead oforder DESC
Book.joins(:comments)
.select("books.*, MAX(comments.created_at) as c_max")
.where(user_id: current_user.id)
.group("books.id")

Will Paginate + Rails with association

I'm having a problem where pagination works properly in displaying the correct number of pages to display all objects, but it's simply repeating all of the same objects on each page. (eg. If there are 10 objects and my per_page is 10, it'll show one page and all 10 items; if per_page is 5, it'll show 2 pages, but in both cases it keeps repeating ALL of objects from 1-10 on every page)
There is a HMBTM relationship, and all images associated in this case with the product model is in a join model.
I'm using this pagination gem: https://github.com/mislav/will_paginate/wiki
Products Controller
def show
#products = #product.images.all.page(params[:page]).per_page(10)
end
View
<% #product.images.each_slice(2) do |f,g| %>
...
<% end %>
<%= will_paginate #products %>
I already defined #products in the controller so I had to adjust my view so it didn't repeat the same code and just used the defined instance variable.
<% #products.each_slice(2) do |f,g| %>
...
<% end %>
<%= will_paginate #products %>

List all posts on Show view page - Rails

I'd like to make a list of posts on the app/views/posts/show.html.erb page and sort each by id.
Similar to how all of the posts are listed on my app/views/posts/index.html.erb page using the code block below:
<% #posts.each do |post| %>
<div class="col-md-4">
<%= image_tag post.img %>
<h1><%= post.title %></h1>
<p><%= post.content %></p>
<br>
<%= link_to 'Read More', post_path(post) %>
</div>
<% end %>
When I try to use the same each do method on the show page I get an error. But this is what I currently have (it only displays an img/link to the current post):
<h1>Recent Posts</h1>
<ul>
<li>
<%= image_tag #post.img %>
<h2>
<%= link_to #post.title %>
</h2>
</li>
</ul>
Index is for displaying all the items of x.
def index
#posts = Post.all
end
So what you are doing is taking all your posts, and putting them in an array called #posts. You can iterate or enumerate over those with .each do |x|. That means go through each object in the array and show the post image, title and content.
You didn't display your show, but typically a show looks like:
def show
#post = Post.find(params[:id])
end
So you are finding the post with :id and storing that data in #post. This is only 1 object, it's not an array. That's why your .each do |x| isn't working.
There is nothing stopping you from making
def show
#posts = Post.all
end
But then you can't take advantage of rails shortcuts and are repeating yourself, which isn't good in programming. If you want two very distinct windows that use the same information, it's better to figure that out in html/css with a bit of javascript.
The show action of your PostsController is probably only setting up #post, and not #posts. You can't use .each with #post because it's an instance of Post, and not an array, or something that responds to .each. Look at how #posts is set up in the index action, and copy that to your show action.

Paginate many has_many belongs_to relationships in Rails with will_paginate

A user has_many potatoes. Potatoes belong_to a user. A potato has_many quirks. Quirks belong_to potatoes.
I want to display my potatoes and their associated quirks together in my Users show and Paginate using the will_paginate gem like below.
potato_names -> sweet potato russet potato mystery potato
quirk_name -> shiny rough purple
quirk_name -> big brown orange
quirk_name -> old medium blue
My users controller pre-pagination
UsersController
def show
#user = User.find(params[:id])
#potatoes = #user.potatoes
#quirks = Quirk.where(potato: #potatoes)
end
I can paginate potatoes by changing the UsersController -
#potatoes = #user.potatoes.paginate(page: params[:page])
and Users - show.html.erb
<%= #potatoes.potato_name %>
<%= will_paginate #potatoes %>
but how can I get quirks to paginate with the potatoes so that when I click next I get the potatoes and quirks associated with the next set? Initially I was getting potatoes to paginate and quirks to just show itself, which is why I didn't display quirks previously. I wasn't understanding the .pagination call well enough.
I can call :param_name to specify the parameter name for page number, and make sure that it's working appropriately.
will_paginate documentation
I can do
UsersController
def show
#user = User.find(params[:id])
#potatoes = #user.potatoes.paginate(page: params[:potatoes_page])
#quirks = Quirk.where(potato: #potatoes).paginate(page: params[:quirks_page])
end
show.html.erb
<%= render #potatoes %>
<%= render #quirks %>
<%= will_paginate #potatoes, :params_name => 'potatoes_page' %>
<%= will_paginate #quirks, :params_name => 'quirks_page' %>
_potato.html.erb
<%= potato.potato_name %>
_quirk.html.erb
<%= quirk.quirk_name %>
This should give me potatoes and quirks paging separately... But when I next page potatoes, potatoes is paging and quirks is disappearing. When I next page quirks page, potatoes stays the same and only quirks changes.
I believe that the above error is because I'm calling pagination in the potatoes query, then using the potatoes query to call pagination again.
The overall look should be:
UsersController
def show
#user = User.find(params[:id])
#potatoes = #user.potatoes.paginate(page: params[:page])
#quirks = Quirk.where(potato: #user.potatoes).paginate(page: params[:page])
end
show.html.erb
<%= render #potatoes %>
<%= render #quirks %>
<%= will_paginate #potatoes, :params_name => 'page' %>
_potato.html.erb
<%= potato.potato_name %>
_quirk.html.erb
<%= quirk.quirk_name %>
The error was in calling pagination on a pagination with #potatoes and #quirks in the UsersController. Additionally I changed :params_name back to the default page so both models would change at the same time. To change them separately, use potatoes_page and quirks_page as shown in the question. I also removed
<%= will_paginate #quirks %>
because I was getting two next bars. If you are changing #potatoes and #quirks separately, you would need to include it.

rails 4 populate dropdown values from database

I have a dropdown in a rails form:
<%= f.select :lists, [["test1", 1], ["test2", 0]] %>
This works fine but how can I make it dynamic. (interacting with model data)
I have a controller with an action containing #list = List.all
How can I populate id and name in my combobox. I've been searching around, but I am unclear about it. Can anyone help>
You can use options_from_collection_for_select.
<% options = options_from_collection_for_select(#list, 'id', 'name') %>
<%= f.select :all_val, options %>
Don't quite have enough reputation to respond to your question in the thread above #learner but there's a good chance that #overflow didn't have #list defined in his controller.
To solve my case I put my equivalent of #list (in this case #restaurants) in my "def new" method since I was using it to help create new items with associated restaurants.
# GET /specials/new
def new
#special = Special.new
#restaurants = Restaurant.all // Look Here
end
Additionally, :all_val in the original response should be the parameter you want to pass in to the database. In my case it was :restaurant_id
This worked for me
# view
<%= form.select(:list_id) do %>
<% #list.each do |l| -%>
<%= content_tag(:option, l.name, value: l.id) %>
<% end %>
<% end %>
and
# controller
#list ||= List.all
`

Resources