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

**URL:** <https://www.ruby-forum.com/t/manage-1-ressource-with-2-views-the-restful-way/146840>\
**Category:** Rails\
**Created:** [August 26, 2008, 5:10am UTC](https://www.ruby-forum.com/t/manage-1-ressource-with-2-views-the-restful-way/146840 "2008-08-26T05:10:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregory\_barborini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@gregory\_barborini](https://www.ruby-forum.com/u/gregory_barborini)\
**Post date:** [August 26, 2008, 5:10am UTC](https://www.ruby-forum.com/t/manage-1-ressource-with-2-views-the-restful-way/146840/1 "2008-08-26T05:10:55Z")

</div>

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 🙂

Greg

---

<div class="post-metadata">

**Author:** ![gregory\_barborini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@gregory\_barborini](https://www.ruby-forum.com/u/gregory_barborini)\
**Post date:** [August 26, 2008, 5:45am UTC](https://www.ruby-forum.com/t/manage-1-ressource-with-2-views-the-restful-way/146840/2 "2008-08-26T05:45:23Z")

</div>

On Aug 25, 10:10 pm, gregory barborini [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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 🙂 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 🙂
> 
> 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](http://www.purpleworkshops.com/workshops/rest-and-web-services)  
Oct 4, 2008, Austin, TX, $295. Use discount code “PWRRGG” for 10%  
off.

---

<div class="post-metadata">

**Author:** ![gregory\_barborini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@gregory\_barborini](https://www.ruby-forum.com/u/gregory_barborini)\
**Post date:** [August 26, 2008, 5:51am UTC](https://www.ruby-forum.com/t/manage-1-ressource-with-2-views-the-restful-way/146840/3 "2008-08-26T05:51:14Z")

</div>

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

Greg
