Skip to Content
Ruby on Rails® for Microsoft Developers
book

Ruby on Rails® for Microsoft Developers

by Antonio Cangiano
April 2009
Intermediate to advanced content levelIntermediate to advanced
479 pages
12h 55m
English
Wrox
Content preview from Ruby on Rails® for Microsoft Developers

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby on Rails™ 3 Tutorial: Learn Rails™ by Example

Ruby on Rails™ 3 Tutorial: Learn Rails™ by Example

Michael Hartl

Publisher Resources

ISBN: 9780470374955Purchase book