Reputation: 172
I have a method in my posts_controller to display most viewed posts.
def top
@posts = Post.all.order("post.views DESC").page(params[:page]).per(10)
end
In routes I have
resources :posts do
collection do
get :top
end
end
The problem is: when i go to /posts/top i have an error: Missing template posts/top, application/top
Do I need to write view files for my every method (top isn't the only one) or I can somehow display them in my index file without duplication of code?
Upvotes: 0
Views: 116
Reputation: 8434
I would suggest you to have a close look to rails layouts and rendering documentation. You will get your answer as well concept behind them. Below is the snippet of doc.
In most cases, the ActionController::Base#render method does the heavy lifting of rendering your application's content for use by a browser. There are a variety of ways to customize the behavior of render. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.
Upvotes: 0
Reputation: 27473
Just render the index
template at the end of your method:
def top
@posts = Post.all.order("post.views DESC").page(params[:page]).per(10)
render :index
end
Upvotes: 1