O'Reilly logo

Ruby on Rails® for Microsoft Developers by Antonio Cangiano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.13. Adding a Custom REST Action

At this point in time, any scheduled article or draft will be excluded from the homepage. You could edit them from the console or access them by placing their id in the URL, but this is far from convenient. What you can do is create a custom action called unpublished. Add the following action to your ArticlesController:

# GET /articles/unpublished
# GET /articles/unpublished.xml
def unpublished
  @articles = Article.unpublished.find(:all, :order => "published DESC, published_at DESC")

  respond_to do |format|
    format.html { render :action => "index" }
    format.xml  { render :xml => @articles }
  end
end

To retrieve the list of unpublished articles, you can chain the unpublished named scope with a finder that sorts them to display scheduled articles first and drafts second. Furthermore, you keep the inverse chronological order for each of the two "groups."

The template for the unpublished action would be identical to the one of the index action because they both display a bunch of articles, no matter what these are.

Truth be told, the destroy action redirects every HTML request to the index action. This means that if you delete an article from the "unpublished page" you'll be redirected back to the index action, not to unpublished. It's a minor nuisance that can be fixed by improving the redirecting logic inside the destroy action, to make it aware of the previous action (for example, index or unpublished). To do so, replace redirect_to(articles_url) in ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required