Manage 1 ressource with 2 views, the "Restful way"

Hello all,
I’m working on a website listing recipes. I need 2 views :

  • the first will be my “Home” page with the last 6 recipes and much
    details.
  • the second is a listing of all the recipes sorted by alphabetical
    order.
    I’d like to find a Restful solution…
    But how can i manage :
  • 1 controller with 2 methods like show and show_last_recipes ?( … I
    don’t think it’s very Restful)
  • 2 views with 1 controller and depending on a “display” variable my
    controller will choose between the 2 views…
    I’m not very happy with those 2 solutions.
    Do you have any ideas ? Thanks :slight_smile:

Greg

On Aug 25, 10:10 pm, gregory barborini [email protected]
wrote:

Hello all,
I’m working on a website listing recipes. I need 2 views :

  • the first will be my “Home” page with the last 6 recipes and much
    details.
  • the second is a listing of all the recipes sorted by alphabetical
    order.
    I’d like to find a Restful solution…
    But how can i manage :
  • 1 controller with 2 methods like show and show_last_recipes ?( … I
    don’t think it’s very Restful)

I agree :slight_smile: Custom actions are usually a last resort for me.

  • 2 views with 1 controller and depending on a “display” variable my
    controller will choose between the 2 views…
    I’m not very happy with those 2 solutions.
    Do you have any ideas ? Thanks :slight_smile:

Greg

First, it’s a myth that you’re not allowed to have a “home” controller
to serve as your home page. This controller can render as many
resources, in whatever fashion, and it deems appropriate. The home
page is a kind of resource unto itself, and can be treated restfully.

With that in mind, here’s one way to proceed:

Add a HomeController, with an index action, to serve as your home
page:

routes.rb

map.root :controller => ‘home’

home_controller.rb

def index
@recipes = Recipe.find(:all, :limit => 6, :order => ‘id desc’)

end

Your RecipesController can be fully RESTful as you would expect, using
map.resources :recipes, etc.

If you’re concerned about redundant code in your views, first write
your views and see what happens. If they are indeed duplicating code,
you can just factor out the reusable stuff into partials that you can
share among controllers.

Hope this helps…?

Jeff

REST with Rails: Do cool things with Rails! For beginning Rails
developers or those just new to REST.
http://www.purpleworkshops.com/workshops/rest-and-web-services
Oct 4, 2008, Austin, TX, $295. Use discount code “PWRRGG” for 10%
off.

This is exactly the solution i was searching !
Thanks so much !

Greg