I have
#microposts = Micropost.order('votes desc').paginate(:page => params[:page])
and it works but I want to convert it to a scope such that I can call
# #microposts = Micropost.all.paginate(:page => params[:page])
and have the same output.
In micropost.rb, I have
scope :order => 'votes desc'
but that doesn't work. The error I receive is:
undefined method `to_sym' for {:order=>"votes desc"}:Hash
Can someone explain to me what is going on?
Thanks.
scope :ordered, order("votes desc")
This also might be a possible valid case for a default_scope too.
Related
I am able to install this app on my windows 7 laptop https://github.com/cheezy/puppies
But now when I am trying to access it at localhost:3000 it is giving me error:
unsupported parameters: :order
I went to the file in this app and found this code:
app/controllers/agency_controller.rb
class AgencyController < ApplicationController
skip_before_filter :authorize
def index
#puppies = Puppy.paginate :page => params[:page], :order => 'name', :per_page => 4
end
end
While looking for a fix on this error I found a fix here https://github.com/mislav/will_paginate/issues/500
Where a comment by "mislav" says that "Active Record doesn't support :xyz formatting and it need to be written in User.where(conditions).order('title').per_page(per_page).page(page) format.
So, if it is a fix how to write #puppies = Puppy.paginate :page => params[:page], :order => 'name', :per_page => 4 in suggested format?
But if its not actual fix how to fix it?
I think that's because you are passing :order into the paginate method.
The correct query should be:
#puppies = Puppy.order(:name).paginate(page: params[:page], per_page: 4)
I get DEPRECATION WARNING: #apply_finder_options is deprecated. when trying this in my user.rb:
def User.search(search, page)
paginate page: page,
per_page: 10,
conditions: ['name LIKE ?', "%#{search}%"],
order: 'name'
end
Called through UsersController:
def index
#users = User.search(params[:search], params[:page])
end
The pagination is done with the will_paginate gem.
What is triggering the warning and how can I fix it? Been trying some googling, but I find the docs not too comprehensive!
I'm pretty sure you just need to pull the order and conditions options out of the paginate method and use Active Record for that instead:
def User.search(search, page)
order('name').where('name LIKE ?', "%#{search}%").paginate(page: page, per_page: 10)
end
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)
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
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