For instance, I have got a Comapny model. I can get per page limit of 10 in the way as below?
class Client::CompanyController < ApplicationController
def company_search
#companies = Company.all.page(params[:page],:per_page=>10)
end
end
You can user will_paginate gem
and than in your controller You can put:
def company_search
#companies = Company.all.paginate(page: params[:page], per_page: 10)
end
and in Your view:
# render your companies data with render or #companies.each do |company|
render #companies
# pagination
will_paginate #companies
Related
Sorry, this may seem simple.
I'm using 'render #orders' but I need only the orders where that have the Storeid = current_user
I'd normally do
Order.where(Storeid: current_user).each do |order|
ect ect
But I'm unsure how to do this with render.
Filter the #orders instance variable in your controller:
class OrdersController < ApplicationController
def index
#orders = Order.where(store_id: current_user.id)
end
end
Then in your view app/views/orders/index.html.erb:
<%= render #orders %>
So in my dashboard I want for separate modules to have the normal will_paginate settings. However I have a module that pulls in data from all other models for search/filter. The problem at the moment is that the normal will_paginate settings is also affecting the search/filter. So for example Books currently has 300 items. When I use the search/filter in my Search Index it limits to only 30 based on will_paginate. Without hitting the models how do I increase the will_paginate on the Search Index alone?
Search Controller looks like:
class Admin::SearchesController < Admin::ApplicationController
belongs_to_app :search
add_breadcrumb 'Search', :admin_searches_path
respond_to :html, :json
def index
end
def all_users
#users = User.includes(
:user_books, :books
).all
render 'all_users'
end
def all_books
respond_with UserBook.ordered
end
end
Books Controller
class Admin::BooksController < Admin::ApplicationController
belongs_to_app :books
add_breadcrumb 'Books', :admin_books_path
before_action :load_book, only: [:show, :edit, :update, :destroy]
respond_to :html, :json
def index
#books = Book.paginate(page: params[:page])
#books = #books.search(params[:search]) if params[:search]
respond_with(#books)
end
end
Then I'm using a jbuilder file
json.call(#users) do |user|
json.name user.first_name + ' ' + user.last_name
json.email user.email
json.books(user.user_books) do |user_book|
json.id user_book.book.id
json.name user_book.book.name
end
end
Then pulling on the index with the following:
mounted: function() {
var self = this;
$.getJSON('/admin/all_users/').then(function(json) {
self.allUsers = json;
});
$.getJSON('/admin/books/').then(function(json) {
self.allBooks = json;
});
I've tried playing around with the controller for Book to limit the page but that really didn't work and it was affecting the Books page which I definitely don't want. Just the results.
Edit:
Another option I'm trying to play around is putting in application_record.rb is WillPaginate.per_page = 999999.
This resolves the issue on the Search Index page on pulling in all Books. However the Books page is stripped of it's pagination. So then I add in on the controller:
#books = Book.paginate(page: params[:page], per_page: 10)
This then limits the Search Index on pulling in all books and it's now limited to 10.
Is there a way for the ajax request to change that number based on the page?
Micropost has content that I want in the index (#user.micropost.content). I can do this in the show but not in index.
My models:
class User < ActiveRecord::Base
has_many :microposts
end
class Micropost < ActiveRecord::Base
belongs_to :user
end
My user_controller.rb:
def index
#users = User.all
end
def show
#user = User.find(params[:id])
#posts = #user.microposts
end
First you need a little eager loading to avoid N+1 queries
def index
#users = User.includes(:microposts).all
end
Then in the view you just loop normally and you'll be able to access the object
#users.each do |user|
user.microposts.each do |micropost|
micropost
end
end
Try with this code in your users controller:
def index
#users = User.includes(:microposts).all
end
And in your index page you show the microposts using:
<% #user.microposts do |micropost| %>
<p><%= micropost.title%></p>
<% end %>
This will allow you to use your users' microposts in the view and is also a good practice because the query in the database for the microposts will be executed only once.
You will have to loop through #users = User.all and access the microposts from each user that way.
def index
#users = User.all
#users.each do |user|
current_users_microposts = user.microposts
# your code here
end
end
Not sure what your requirements are but you can make an array and append each group of user.microposts onto that array as well, in order to have all of them in one variable.
I have been following this tutorial to get the leaderboard gem working and I've got all of the leaderboard gem aspect of it working, but I am struggling to get the Kaminari gem part of it working (the paginate gem).
At the moment in my controller I have this:
class LeaderboardController < ApplicationController
before_action :query_options
def show
#lb = Boards.default_leaderboard
#entries = entry_service.execute(query_options)
respond_to do |format|
format.html do
paginate
end
format.json do
render json: #entries
end
end
end
private
def query_options
#limit = [params.fetch(:limit, 10).to_i, 100].min
#page = params.fetch(:page, 1).to_i
{ page: #page, limit: #limit }
end
def paginate
pager = Kaminari.paginate_array(
#entries,
total_count: #lb.total_members)
#page_array = pager.page(#page).per(#limit)
end
And in the views I have:
= paginate #page_array
But for some reason this is not paginating the #entries...
For the way you have it set up you need to call the before_action :paginate.
I'm new to Rails, and I'm having major trouble getting will_paginate to work with a nested resource.
I have two models, Statement and Invoice. will_paginate is working on Statement, but I can't get it to work on Invoice. I know I'd doing something silly, but I can't figure it out and the examples I've found on google won't work for me.
statement.rb
class Statement < ActiveRecord::Base
has_many :invoices
def self.search(search, page)
paginate :per_page => 19, :page => page,
:conditions => ['company like ?', "%#{search}%"],
:order => 'date_due DESC, company, supplier'
end
end
statements_controller.rb <irrelevant code clipped for readability>
def index #taken from the RAILSCAST 51, will_paginate podcast
#statements = Statement.search(params[:search], params[:page])
end
I call this in the view like so, and it works:
<%= will_paginate #statements %>
But I can't figure out how to get it to work for Invoices:
invoice.rb
class Invoice < ActiveRecord::Base
belongs_to :statement
def self.search(search, page)
paginate :per_page => 19, :page => page,
:conditions => ['company like ?', "%#{search}%"],
:order => 'employee'
end
end
invoices_controller.rb
class InvoicesController < ApplicationController
before_filter :find_statement
#TODO I can't get will_paginate to work w a nested resource
def index #taken from the RAILSCAST 51, will_paginate podcast
#invoices = Invoice.search(params[:search], params[:page])
end
def find_statement
#statement_id = params[:statement_id]
return(redirect_to(statements_url)) unless #statement_id
#statement = Statement.find(#statement_id)
end
end
And I try to call it like this:
<%= will_paginate (#invoices) %>
The most common error message, as I play with this, is:
"The #statements variable appears to be empty. Did you forget to pass the collection object for will_paginate?"
I don't have a clue what the problem is, or how to fix it. Thanks for any help and guidance!
Solved -
I moved the invoices pagination into Statement's controller, like this:
def show
#statement = Statement.find(params[:id])
#TODO move the :per_page stuff out to a constant
#invoices = #statement.invoices.paginate :per_page => 10,
:page => params[:page],
:order => 'created_at DESC'
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #statement }
end
end
and call it in the view like this (code trimmed for readability>
<div id="pagination">
<%= will_paginate #invoices %>
</div>
<table>
<%# #statement.invoices.each do |invoice| -
shows all invoices with no pagination,
use #invoices instead%>
<%
#invoices.each do |invoice|
%>