If I have a template (.rhtml) that lists displays all records, and I want to filter the list, say a todo list and only show "done" items I want to use the same template and not have to create a "done.rhtml" and a "notdone.rhtml" for the filtering - that''s three templates when one should work. /todo displays all items /todo/done displays only done items /todo/notdone displays items still to be done All of these paths should use the one simple template. Where do you override the template displayed? If there is a better way of going about this? -- Craig Beck http://luckybonza.com AIM: Kreiggers
I''d do the filtering in the controller. For example, if you have an AR object called Items, and the varialbe you use in the template is @items, then each method in the coltroller can filter at the model level and return the filtered data in the @items variable to the template. Then, call render (http://rails.rubyonrails.com/classes/ActionController/Base.html#M000154) to use {APP_BASE}/views/todo/todo.rhtml for each action. e.g.: todo_controller.rb: def index #render all for the default all render_action ''all'' end def all @items = Items.find_all render ''todo/todo'' end def done @items = Items.find_all "is_done = 1" render ''todo/todo'' end def notdone @items = Items.find_all "is_done = 0" render ''todo/todo'' end Then your urls would be: /todo (same as /todo/all) /todo/done /todo/notdone That should do it! :) -Nate On Apr 5, 2005 5:29 PM, Craig Beck <craigbeck-sUNYKzuJffo33s2pWanAEQ@public.gmane.org> wrote:> If I have a template (.rhtml) that lists displays all records, and I > want to filter the list, say a todo list and only show "done" items I > want to use the same template and not have to create a "done.rhtml" and > a "notdone.rhtml" for the filtering - that''s three templates when one > should work. > > /todo displays all items > /todo/done displays only done items > /todo/notdone displays items still to be done > > All of these paths should use the one simple template. Where do you > override the template displayed? > > If there is a better way of going about this? > > -- > Craig Beck > http://luckybonza.com > AIM: Kreiggers > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
In your case use render_action "index" or whatever the default template is. http://api.rubyonrails.com/ -Jeff ----- Original Message ----- From: "Craig Beck" <craigbeck-sUNYKzuJffo33s2pWanAEQ@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Tuesday, April 05, 2005 4:29 PM Subject: [Rails] re-using templates> If I have a template (.rhtml) that lists displays all records, and I > want to filter the list, say a todo list and only show "done" items I > want to use the same template and not have to create a "done.rhtml" and > a "notdone.rhtml" for the filtering - that''s three templates when one > should work. > > /todo displays all items > /todo/done displays only done items > /todo/notdone displays items still to be done > > All of these paths should use the one simple template. Where do you > override the template displayed? > > If there is a better way of going about this? > > -- > Craig Beck > http://luckybonza.com > AIM: Kreiggers > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Wednesday, April 6, 2005, 8:57:42 AM, Nathan wrote:> def index > #render all for the default > all > render_action ''all'' > end> def all > @items = Items.find_all > render ''todo/todo'' > endIn that code, #index calls #all, which calls "render ''todo/todo''". Does #all actually return to #index such that "render_action ''all''" can be called? I guess I''ll just read the API docs for render and render_action, but I''ve read them before and obviously don''t fully get them. Oh well... Gavin
render_action is just a shortcut for render. It fills in the last part of the path with the controller name. E.g. render ''todo/todo'' is the same as render_action ''todo'' (as long as you''re calling it from todo_controller.rb :)). I probably shouldn''t have mixed the two. It is a little confusing. I just yanked some of that code out of one of my apps. The docs do explain it, though. :) On Apr 5, 2005 6:30 PM, Gavin Sinclair <gsinclair-81uBx+iSpXA0n/F98K4Iww@public.gmane.org> wrote:> On Wednesday, April 6, 2005, 8:57:42 AM, Nathan wrote: > > > def index > > #render all for the default > > all > > render_action ''all'' > > end > > > def all > > @items = Items.find_all > > render ''todo/todo'' > > end > > In that code, #index calls #all, which calls "render ''todo/todo''". > Does #all actually return to #index such that "render_action ''all''" > can be called? > > I guess I''ll just read the API docs for render and render_action, > but I''ve read them before and obviously don''t fully get them. Oh > well... > > Gavin > >
Thanks, I was getting lost in the docs wondering where I was supposed to be looking for the solution. Problem solved, and I''ve read a lot of documentaion too boot, so I''ve picked up a few more things on the way... On Apr 5, 2005, at 3:57 PM, Nathan Eror wrote:> I''d do the filtering in the controller. For example, if you have an > AR object called Items, and the varialbe you use in the template is > @items, then each method in the coltroller can filter at the model > level and return the filtered data in the @items variable to the > template. Then, call render > (http://rails.rubyonrails.com/classes/ActionController/ > Base.html#M000154) > to use {APP_BASE}/views/todo/todo.rhtml for each action. e.g.: > > todo_controller.rb: > > def index > #render all for the default > all > render_action ''all'' > end > > def all > @items = Items.find_all > render ''todo/todo'' > end > > def done > @items = Items.find_all "is_done = 1" > render ''todo/todo'' > end > > def notdone > @items = Items.find_all "is_done = 0" > render ''todo/todo'' > end > > Then your urls would be: > /todo (same as /todo/all) > /todo/done > /todo/notdone > > That should do it! :) > > -Nate > > On Apr 5, 2005 5:29 PM, Craig Beck <craigbeck-sUNYKzuJffo33s2pWanAEQ@public.gmane.org> wrote: >> If I have a template (.rhtml) that lists displays all records, and I >> want to filter the list, say a todo list and only show "done" items I >> want to use the same template and not have to create a "done.rhtml" >> and >> a "notdone.rhtml" for the filtering - that''s three templates when one >> should work. >> >> /todo displays all items >> /todo/done displays only done items >> /todo/notdone displays items still to be done >> >> All of these paths should use the one simple template. Where do you >> override the template displayed? >> >> If there is a better way of going about this? >> >> -- >> Craig Beck >> http://luckybonza.com >> AIM: Kreiggers >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Craig Beck http://luckybonza.com AIM: Kreiggers