Adding Ransack Gem with Categories - ruby-on-rails

I am displaying a list of gyms, and users can view gyms by province. I would like to add search capability using the Ransack gem, but I am not sure how to go about it with my current set up. Looking for some advice.
Gyms controller
def index
if params[:province].blank?
#gyms=Gym.all.order("created_at desc").paginate(:page => params[:page], :per_page => 18)
else
#province_id = Province.find_by(name: params[:province]).id
#gyms = Gym.where(province_id: #province_id).order("created_at DESC").paginate(:page => params[:page], :per_page => 18)
end
end
thank you in advance.

Figured it out. Just replaced my existing controller code with this:
#q = Gym.ransack(params[:q])
#gyms = #q.result.includes(:province).page(params[:page]).order("created_at DESC").paginate(:page => params[:page], :per_page => 18)

Related

ActiveRecord Relation error

def index
#users = User.order("name").page(params[:page]).per_page(5)
end
This is my UsersController where i try to view all users from database. Problem is because is see only this error:
undefined method `page' for #<ActiveRecord::Relation::ActiveRecord_Relation_User:0x000000044853d8>
Where is the problem?
You can do like this incase you have used will_paginate gem
#users = User.order("name ASC").paginate(:page => params[:page], :per_page => 5)
Take a look on the gem will_paginate
The usage of will_paginate is slight different than Kaminari. You should do:
#users = User.order('name').paginate(page: params[:page], per_page: 5)

Paginate using with_exclusive_scope

Is there a way in which we can paginate using with_exclusive_scope in Rails 2.3?
#albums = Album.paginate(:page => 1, :per_page => 12, :conditions => cond)
works fine.
Album.with_exclusive_scope {find(:all, :conditions => cond)}
works fine.
Can we combine with_exclusive_scope and paginate?
#albums = Album.find_allx(opts).paginate(:page => 1, :per_page => 12)
with_exclusive_scope is a protected method, so it has to be called from a class message. In this case the class method is find_allx()

Ruby on Rails: will_paginate is not working correctly

If I click the next or previous link it doesn't go to the next or previous page.
All posts are on same page, but there are links next, previous at bottom.
In PostsController:
#posts = Post.paginate(:per_page => 15, :page => params[:page], :order => 'created_at DESC')
in posts/index:
<%= will_paginate #posts%>
Where is the problem with will_paginate?
You have to order before paginate,
so, change it to
#posts = Post.order('created_at DESC').paginate(:per_page => 15, :page => params[:page])
Not sure if this is causing your error, but ordering should be done outside of will_paginate.
#posts = Post.paginate(:per_page => 15, :page => params[:page]).order('created_at DESC')
This is how it should be done in Rails 3.
I've also had trouble setting the per_page parameter within the controller. You could try setting it in the model instead.
class Post
self.per_page = 10
end
Updating to will_paginate , '3.1.7' solved my issues

unknown action with will_paginate

In my users controller I have this in a method:
#users = User.paginate :page => params[:page], :per_page => 10,
The results are rendered on users/search. The 2nd page link points to users/search?page=2, but it leads to an unknown action error.
Possibly there is something wrong in routes.rb, so you haven't got :search=>:get rout

Rails: Showing 10 or 20 or 50 results per page with will_paginate how to?

me again...
I need show 10 or 20 or 50 results number of results per page with a select options in my list of posts using will_paginate plugin
Can you help me please?
Thanks!
Looks like the OP also asked here: http://railsforum.com/viewtopic.php?id=33793 and got much better answers.
To adapt the best solution there, here's what I like:
(in the view)
<%= select_tag :per_page, options_for_select([10,20,50], params[:per_page].to_i),
:onchange => "if(this.value){window.location='?per_page='+this.value;}" %>
(in the controller)
#per_page = params[:per_page] || Post.per_page || 20
#posts = Post.paginate( :per_page => #per_page, :page => params[:page])
To set a class wide Default
class Post < ActiveRecord::Base
def self.per_page
25
end
end
Or on a query by query basis use the per_page in your call
class Post <ActiveRecord::Base
def self.posts_by_paginate
paginate(:all, :per_page => 25, :conditions => ["published = ?", true])
end
end
Here is what I will do
Class UsersController < ApplicationController
def index
#users = User.paginate(:all, :page => params[:page], :per_page => params[:number_of_records])
end
end

Resources