Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

I want to sort the topics newest first. where in the controller would I add this

class TopicsController < ApplicationController
def show @topic = Topic.find(params[:id]) @topic.hit! if @topic end

def new @forum = Forum.find(params[:forum_id]) @topic = Topic.new end

def create @forum = Forum.find(params[:forum_id]) @topic = @forum.topics.build(params[:topic]) @topic.user = current_user

if @topic.save
  flash[:notice] = "Topic was successfully created."
  redirect_to topic_url(@topic)
else
  render :action => 'new'
end

end

def edit @topic = Topic.find(params[:id]) end

def update @topic = Topic.find(params[:id])

if @topic.update_attributes(params[:topic])
  flash[:notice] = "Topic was updated successfully."
  redirect_to topic_url(@topic)
end

end

def destroy @topic = Topic.find(params[:id])

if @topic.destroy
  flash[:notice] = "Topic was deleted successfully."
  redirect_to forum_url(@topic.forum)
end

end end

Jason Seifer

2 Answers

still need an answer

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Sorting would generally be in the index method. You would probably want to do something similar to:

Topic.order("updated_at desc").all