ruby : unsupported parameters: :order - ruby-on-rails

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)

Related

Replacing Will_paginate with Kaminari

I am replacing will_paginate with Kaminari and large part of my application uses .paginate method with multiple conditions.
#posts = Post.published.paginate(:order => 'published_at desc, id',
:joins => :comments,
:conditions => conditions,
:group => "posts.id",
:per_page => 10,
:page => params[:page])
I am looking for quickest way and the most efficient way to replace such code with kaminari.
Rather then
#posts = Post.published.paginate.order('published_at desc').....page().per()

How do I turn Model.order('attributes desc') into a scope?

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.

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

Undefined method 'total_entries' after upgrading Rails 2.2.2 to 2.3.5

I am upgrading a Rails application from 2.2.2 to 2.3.5. The only remaining error is when I invoke total_entries for creating a jqgrid.
Error:
NoMethodError (undefined method `total_entries' for #<Array:0xbbe9ab0>)
Code snippet:
#route = Route.find(
:all,
:conditions => "id in (#{params[:id]})"
) {
if params[:page].present? then
paginate :page => params[:page], :per_page => params[:rows]
order_by "#{params[:sidx]} #{params[:sord]}"
end
}
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #route }
format.json { render :json => #route }
format.jgrid {
render :json => #route.to_jqgrid_json(
[
:id, :name
],
params[:page],
params[:rows],
#route.total_entries
)
}
end
Any ideas? Thanks!
EDIT
I am able to get it working by removing the block used after find. I also had to move the order_by used by the squirrel plugin as I was getting an undefined method call for it.
I don't like the fact that this is less DRY than the previous code by having to use conditions in more than one location. Is there any better way to do this with Rails 2.3.5, will_paginate, and squirrel?
if params[:page].present? then
#route = Route.paginate :conditions => "id in (#{params[:id]})", :page => params[:page], :per_page => params[:rows], :order => "`#{params[:sidx]}` #{params[:sord]}"
else
#route = Route.find(:all, :conditions => "id in (#{params[:id]})")
end
EDIT 2
Another possibility for this error may be that I was using Ruby 1.8.7 with Rails 2.2.2 and am now using Ruby 1.9.1 with Rails 2.3.5. Were there any major changes between 1.8.7 and 1.9.1 that would prevent the block after the ActiveRecord find to not run?
In Rails 2.3.5, a find(:all, ...) call will return an Array and generally these do not have any custom methods associated with them like you might get with a scope. Passing a block to a find call is also a little irregular and may be part of the problem.
You may be able to fix this by creating a scope that finds what you want:
class Route < ActiveRecord::Base
named_scope :with_ids, lambda { |*ids| {
:conditions => { :id => ids.flatten }
}}
end
Then you can use the scope instead:
#routes = Route.with_ids(params[:id])

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