Reputation: 172
I have posts controller with action index :
def index
if params[:tag]
@posts = Post.all.tagged_with(params[:tag]).order("created_at DESC").page(params[:page]).per(10)
elsif params[:top]
@posts = Post.all.top_posts.page(params[:page]).per(10)
else
@posts = Post.all.order("created_at DESC").page(params[:page]).per(10)
end
end
also I have a scope in my Post model:
scope :top_posts, lambda {order("posts.view DESC")}
When I go to http://url/posts/top
i get this: Couldn't find Post with id=top
How can I implement top posts feature to blog? And what i am doing wrong?
Upvotes: 0
Views: 161
Reputation: 17919
routes.rb
resources :posts do
collection do
get :top
get :tag
end
end
posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.all.order("created_at DESC").page(params[:page]).per(10)
end
def top
@posts = Post.all.top_posts.page(params[:page]).per(10)
end
def tag
@posts = Post.all.tagged_with(params[:tag]).order("created_at DESC").page(params[:page]).per(10)
end
end
Upvotes: 1
Reputation: 9025
In your config/routes.rb
: put the route for your top products above the one for your show action, something like:
match '/posts/top', :to => 'posts#index', :as => :top, :top => true
...
match '/posts/:id', :to => 'posts#show', :as => :post
Or, if you are using resource routing, resource route should go after your "top posts" route.
Upvotes: 0