gregory barborini
2008-Aug-26 03:10 UTC
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 :) Greg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Aug 25, 10:10 pm, gregory barborini <gregory.barbor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 :) > > GregFirst, 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gregory barborini
2008-Aug-26 03:49 UTC
Re: Manage 1 ressource with 2 views, the "Restful way"
This is exactly the solution i was searching ! Thanks so much ! Greg On Aug 26, 5:44 am, Jeff <cohen.j...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 25, 10:10 pm, gregory barborini <gregory.barbor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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 > Oct 4, 2008, Austin, TX, $295. Use discount code "PWRRGG" for 10% > off.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---