I used will_paginate to paginate, now I want to control how many records in one page.
I do know how to interact data from the select_tag. please tell me how to return the data to
#page in controller.
I used
<td><%= select_tag "count", "<option>10</option> <option>20</option>".html_safe%></td>
Controller
def index
#page = 10;
#users = User.order(:username).joins(:biography).where("`is_active?` = true AND `last_sign_in_at` > DATE_SUB(NOW(), INTERVAL 6 MONTH) ").paginate(:page => params[:page], :per_page => #page)
end
You can use the params[:count] like this to get the number of records per page based on the value selected in the select_tag
def index
#page = params[:count] #here
#users = User.order(:username).joins(:biography).where("is_active? = true AND last_sign_in_at > DATE_SUB(NOW(), INTERVAL 6 MONTH) ").paginate(:page => params[:page], :per_page => #page)
end
Related
Alrighty, so I have searched on here repeatedly on this as well as other places but have yet to find the solution that relates to me. Probably because the previous questions were some time ago.
My problem is that I had first added pagination and then I was required to add a search so that users could search for products. When they do the search it's just supposed to open the products page. If I took out the search, the pagination doesn't give me an error.
The error I get now is
''undefined method `total_pages' for # Product::ActiveRecord_Relation:''
and the line of code highlighted for the error is the pagination in the index.html.erb.
What am I missing? Thanks for any guidance, this newbie needs it!
This is the products_controller:
def index
if Rails.env == "development"
name_env = "name LIKE ?"
else
name_env = "name ilike ?"
end
if params[:q]
search_term = params[:q]
#products = Product.search(search_term)
else
#products = Product.all
#products = Product.paginate(:page => params[:page], per_page: 4)
end
end
This is the index.html.erb :
<div class="pagination">
<%= will_paginate #products %>
</div>
You have missed paginate method when the search, it #products = Product.search(search_term) will be like this
.....
if params[:q]
search_term = params[:q]
#products = Product.search(search_term).paginate(:page => params[:page], per_page: 4)
else
#products = Product.all.paginate(:page => params[:page], per_page: 4)
.....
Additionally Remove this #products = Product.all don't need this.
After all, you just paste this instead of your code, it reduced
def index
if Rails.env == "development"
name_env = "name LIKE ?"
else
name_env = "name ilike ?"
end
if params[:q]
search_term = params[:q]
#products = Product.search(search_term)
else
#products = Product.all
end
#products = #products.paginate(:page => params[:page], per_page: 4)
end
Modify index action as follows:
def index
if Rails.env == "development"
name_env = "name LIKE ?"
else
name_env = "name ilike ?"
end
#products = params[:q] ? Product.search(params[:q]) : Product.scoped
#products.paginate(:page => params[:page], per_page: 4)
end
you should use paginate for searching too.
I have the following search query for products using searchkick:
def index
#filter = params[:filter].blank? ? nil : Search.find(params[:filter])
if #filter.present?
#products = #filter.products(set_order_column(#sort), #direction, params[:page], #per_page)
else
query = #query.presence || "*"
#products = Product.search(
query,
where: {
active: true
},
page: params[:page],
per_page: #per_page,
order: [{
"#{set_order_column(#sort)}" => "#{#direction}"
}],
misspellings: { distance: 2 },
fields: fields)
end
#filter_product_ids = #products.map(&:id)
end
There is the variable filter_product_ids where I need to store all results not filtered results by #per_page. Is it possible to do that? Right now, there are results just for results #per_page.
The goal to get all the results without pagination is to get uniq values for all products used to various product categorization for filtering on the website.
Thank you for any hints, Miroslav
My solution is to put this in it's own class (service class), then
class ProductFilter
attr_reader :search_params, :search_query, :pagination_params
def initialize(search_params, search_query, pagination_params)
#search_params = search_params
#search_query = search_query
#pagination_params = pagination_params
end
# Will return all records from searchkick, unpaginated
def filtered_products
Product.search(search_query, search_param_stuff)
end
# Will return all filtered_products & then paginate on that object
def paginated_products
filtered_products.records.paginate(pagination_params)
end
private
def search_param_stuff
search_params
end
end
In any case, searchkick's method for pagination actually works on the searchkick records object, so you can just pull the ES query (without passing in pagination params) in one method, then paginate on the filtered_products.records.
Just remove
params[:page], #per_page
and
page: params[:page],
per_page: #per_page,
from your code.
I am using sunspot 2.2.2 in my rails app for searching results,
I have this code for grouping in my model:
def self.search_products(params, currency = nil, page_uri = nil)
search_products = Sunspot.search(VariantPrice) do
group :code do
if params[:gallery_order].present?
order_by :price, params[:gallery_order].downcase.to_sym
elsif params[:new_arrival].present? || params[:name] == "new-arrivals"
order_by :product_created_at, :desc
else
if params[:fashion_type] == "fashion"
order_by :price, :asc
elsif params[:sort] != "lowhigh"
order_by :price, :asc
else
order_by :price, :asc
end
end
limit 1
end
end
and I have this code in my controller :
variant_prices = Product.search_products(params, #currency, request.original_fullpath)
#variant_prices = []
variant_prices.group(:code).groups.each do |group|
group.results.each do |result|
#variant_prices << result
end
end
#variant_prices = #variant_prices.paginate(:page => params[:page], :per_page => PER_PAGE_VALUE)
#variant_prices_count = variant_prices.group(:code).total
now I am getting the expected count that is #variant_prices_count, which is 1400 in my case, but I am getting #variant_prices count as 60 which is wrong in my case , here I was expecting to get 1400.and then I want to paginate with this result. cant understand whether it's will_paginate issue or something else.
Help!
You can get 1400 from the paginate instance also by Total entries
by this way replace count with total_entries
#variant_prices = #variant_prices.paginate(:page => params[:page], :per_page => PER_PAGE_VALUE)
#variant_prices.total_entries#it will return toal entries 1400
i am trying to delete from list but when i am trying this it is getting deleted from database
#course = Course.find(params[:id])
#search = Lesson.search(params[:q])
#lessons = #search.result.paginate(:page => params[:page], :per_page => 10)
#search.build_condition if #search.conditions.empty?
#course.lessons.each do |lesson|
#lessons.each do |l|
if lesson.id == l.id
#lessons.delete(l)
end
end
end
I am getting this error: delete_all doesn't support limit scope
Thanking you
Delete is an ActiveRecord method. I assume you don't want to delete it from the database but from the result list. You can do it like this:
#course.lessons.each do |lesson|
#lesson.reject { |l| l.id == lesson.id }
end
I have a simple search action that has 3 parameters and a where method to search a model. If I search and some of the parameters are nil, it will not return the records I want. I want it to search the database by only using the parameters that are not nil/blank. So if only one category is entered and sent in the parameters, I want my controller to ignore the other two parameters. However, if the other parameters are present, I want them to be included in the search.
I've tried many approaches but I can't get it to work properly. Here's my code.
hash = []
cat = :category_id => params[:category_id]
col = :color_id => params[:color_id]
brand = :brand_id => params[:brand_id]
if params[:category_id].present?
hash += cat
end
if params[:color_id].present?
hash += col
end
if params[:brand_id].present?
hash += brand
end
#results = Piece.where(hash).preload(:item).preload(:user).group(:item_id).paginate(:page => params[:page], :per_page => 9)
I've put the variables into strings and hashs, called to_a, joined them with (","). Nothing works.
Thanks
Try this code.
criteria = { :category_id => params[:category_id], :color_id => params[:color_id],
:brand_id => params[:brand_id] }.select { |key,value| value.present? }
#results = Piece.where(criteria).preload(:item).preload(:user).group(:item_id).
paginate(:page => params[:page], :per_page => 9)