Hey all, Here''s my situation: I have a pair of controllers with associated models (called Services and Testimonials) that are quite similar. Because their CRUD behavior is executed via AJAX, the "templates" for the actions are all short .rjs files. Now, because of the similarity of the models, most of the templates are exactly the same, with only the object names changed. That is, where one looks like this: page.visual_effect :toggle_slide, ''hidden_testimonial_form'' page.visual_effect :toggle_slide, ''add_testimonial_button'' The other looks like this: page.visual_effect :toggle_slide, ''hidden_service_form'' page.visual_effect :toggle_slide, ''add_service_button'' Obviously there''s no reason these should be separate. My question is, what''s the best way to set these up as shared templates? Both of the controllers inherit from a third controller (Brochure) because of shared authentication behavior. But the only way I can think of to share templates is something like this: Put template files in /views/brochure. At the end of each action, explicitly render the template. i.e. def new ... render :template => ''brochure/new end Is there a better way that I''m missing? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I can think of a few things off the top of my head - one way is to have one RJS file that looks like this: page.visual_effect :toggle_slide, "hidden_#{@object}_form" page.visual_effect :toggle_slide, "add_#{@object}_testimonial}_button" And in each controller action, right before rendering the RJS, do this @object = "testimonial" or @object = "service" Basically, use instance variables (or another mechanism) to make things available to the RJS template just like you would with anything else. Heck if you have @service or @testimonial there, you could generalize it more by using the same variable for each - @thing then do @thing.class.downcase to get "testimonial" or "service" How does that sound? :) On Tue, Jan 20, 2009 at 3:42 PM, admanb <admanb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hey all, > > Here''s my situation: I have a pair of controllers with associated > models (called Services and Testimonials) that are quite similar. > Because their CRUD behavior is executed via AJAX, the "templates" for > the actions are all short .rjs files. Now, because of the similarity > of the models, most of the templates are exactly the same, with only > the object names changed. That is, where one looks like this: > > page.visual_effect :toggle_slide, ''hidden_testimonial_form'' > page.visual_effect :toggle_slide, ''add_testimonial_button'' > > The other looks like this: > > page.visual_effect :toggle_slide, ''hidden_service_form'' > page.visual_effect :toggle_slide, ''add_service_button'' > > Obviously there''s no reason these should be separate. My question is, > what''s the best way to set these up as shared templates? Both of the > controllers inherit from a third controller (Brochure) because of > shared authentication behavior. But the only way I can think of to > share templates is something like this: > > Put template files in /views/brochure. > > At the end of each action, explicitly render the template. i.e. > > def new > ... > render :template => ''brochure/new > end > > Is there a better way that I''m missing? > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Actually, I got that part pretty well solved by just changing everything to "widget." :) The part I''m wondering is if there''s some way to re-route most of a controller to a different set of views than the standard default. Right now every action ends with "render :template => ''widget/ action_name''" but I''m curious if there''s a better way. -Adam On Jan 20, 2:03 pm, "Brian Hogan" <bpho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I can think of a few things off the top of my head - one way is to > have one RJS file that looks like this: > > page.visual_effect :toggle_slide, "hidden_#{@object}_form" > page.visual_effect :toggle_slide, "add_#{@object}_testimonial}_button" > > And in each controller action, right before rendering the RJS, do this > > @object = "testimonial" > > or > > @object = "service" > > Basically, use instance variables (or another mechanism) to make > things available to the RJS template just like you would with anything > else. > > Heck if you have @service or @testimonial there, you could generalize > it more by using the same variable for each - @thing > > then do -7vejNnw+SPdsdeCuBIFPOufBYlhhx0k0@public.gmane.org to get "testimonial" or "service" > > How does that sound? :) > > On Tue, Jan 20, 2009 at 3:42 PM, admanb <adm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hey all, > > > Here''s my situation: I have a pair of controllers with associated > > models (called Services and Testimonials) that are quite similar. > > Because their CRUD behavior is executed via AJAX, the "templates" for > > the actions are all short .rjs files. Now, because of the similarity > > of the models, most of the templates are exactly the same, with only > > the object names changed. That is, where one looks like this: > > > page.visual_effect :toggle_slide, ''hidden_testimonial_form'' > > page.visual_effect :toggle_slide, ''add_testimonial_button'' > > > The other looks like this: > > > page.visual_effect :toggle_slide, ''hidden_service_form'' > > page.visual_effect :toggle_slide, ''add_service_button'' > > > Obviously there''s no reason these should be separate. My question is, > > what''s the best way to set these up as shared templates? Both of the > > controllers inherit from a third controller (Brochure) because of > > shared authentication behavior. But the only way I can think of to > > share templates is something like this: > > > Put template files in /views/brochure. > > > At the end of each action, explicitly render the template. i.e. > > > def new > > ... > > render :template => ''brochure/new > > end > > > Is there a better way that I''m missing?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sure. No, there really isn''t. It''s a convenience thing. Not having to specify the template is a blessing in most cases. Thing is, you could probably do simething interesting with before / after filters. On Tue, Jan 20, 2009 at 4:09 PM, admanb <admanb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Actually, I got that part pretty well solved by just changing > everything to "widget." :) > > The part I''m wondering is if there''s some way to re-route most of a > controller to a different set of views than the standard default. > Right now every action ends with "render :template => ''widget/ > action_name''" but I''m curious if there''s a better way. > > -Adam > > On Jan 20, 2:03 pm, "Brian Hogan" <bpho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I can think of a few things off the top of my head - one way is to >> have one RJS file that looks like this: >> >> page.visual_effect :toggle_slide, "hidden_#{@object}_form" >> page.visual_effect :toggle_slide, "add_#{@object}_testimonial}_button" >> >> And in each controller action, right before rendering the RJS, do this >> >> @object = "testimonial" >> >> or >> >> @object = "service" >> >> Basically, use instance variables (or another mechanism) to make >> things available to the RJS template just like you would with anything >> else. >> >> Heck if you have @service or @testimonial there, you could generalize >> it more by using the same variable for each - @thing >> >> then do @thing.class.downcase to get "testimonial" or "service" >> >> How does that sound? :) >> >> On Tue, Jan 20, 2009 at 3:42 PM, admanb <adm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> > Hey all, >> >> > Here''s my situation: I have a pair of controllers with associated >> > models (called Services and Testimonials) that are quite similar. >> > Because their CRUD behavior is executed via AJAX, the "templates" for >> > the actions are all short .rjs files. Now, because of the similarity >> > of the models, most of the templates are exactly the same, with only >> > the object names changed. That is, where one looks like this: >> >> > page.visual_effect :toggle_slide, ''hidden_testimonial_form'' >> > page.visual_effect :toggle_slide, ''add_testimonial_button'' >> >> > The other looks like this: >> >> > page.visual_effect :toggle_slide, ''hidden_service_form'' >> > page.visual_effect :toggle_slide, ''add_service_button'' >> >> > Obviously there''s no reason these should be separate. My question is, >> > what''s the best way to set these up as shared templates? Both of the >> > controllers inherit from a third controller (Brochure) because of >> > shared authentication behavior. But the only way I can think of to >> > share templates is something like this: >> >> > Put template files in /views/brochure. >> >> > At the end of each action, explicitly render the template. i.e. >> >> > def new >> > ... >> > render :template => ''brochure/new >> > end >> >> > Is there a better way that I''m missing? > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Check out the docs for ActionController::Base#view_paths. You can probably monkey with that to get both controllers to look in the same directory. On Jan 20, 4:25 pm, "Brian Hogan" <bpho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sure. No, there really isn''t. It''s a convenience thing. Not having to > specify the template is a blessing in most cases. Thing is, you could > probably do simething interesting with before / after filters. > > On Tue, Jan 20, 2009 at 4:09 PM, admanb <adm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Actually, I got that part pretty well solved by just changing > > everything to "widget." :) > > > The part I''m wondering is if there''s some way to re-route most of a > > controller to a different set of views than the standard default. > > Right now every action ends with "render :template => ''widget/ > > action_name''" but I''m curious if there''s a better way. > > > -Adam > > > On Jan 20, 2:03 pm, "Brian Hogan" <bpho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I can think of a few things off the top of my head - one way is to > >> have one RJS file that looks like this: > > >> page.visual_effect :toggle_slide, "hidden_#{@object}_form" > >> page.visual_effect :toggle_slide, "add_#{@object}_testimonial}_button" > > >> And in each controller action, right before rendering the RJS, do this > > >> @object = "testimonial" > > >> or > > >> @object = "service" > > >> Basically, use instance variables (or another mechanism) to make > >> things available to the RJS template just like you would with anything > >> else. > > >> Heck if you have @service or @testimonial there, you could generalize > >> it more by using the same variable for each - @thing > > >> then do -7vejNnw+SPdsdeCuBIFPOufBYlhhx0k0@public.gmane.org to get "testimonial" or "service" > > >> How does that sound? :) > > >> On Tue, Jan 20, 2009 at 3:42 PM, admanb <adm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> > Hey all, > > >> > Here''s my situation: I have a pair of controllers with associated > >> > models (called Services and Testimonials) that are quite similar. > >> > Because their CRUD behavior is executed via AJAX, the "templates" for > >> > the actions are all short .rjs files. Now, because of the similarity > >> > of the models, most of the templates are exactly the same, with only > >> > the object names changed. That is, where one looks like this: > > >> > page.visual_effect :toggle_slide, ''hidden_testimonial_form'' > >> > page.visual_effect :toggle_slide, ''add_testimonial_button'' > > >> > The other looks like this: > > >> > page.visual_effect :toggle_slide, ''hidden_service_form'' > >> > page.visual_effect :toggle_slide, ''add_service_button'' > > >> > Obviously there''s no reason these should be separate. My question is, > >> > what''s the best way to set these up as shared templates? Both of the > >> > controllers inherit from a third controller (Brochure) because of > >> > shared authentication behavior. But the only way I can think of to > >> > share templates is something like this: > > >> > Put template files in /views/brochure. > > >> > At the end of each action, explicitly render the template. i.e. > > >> > def new > >> > ... > >> > render :template => ''brochure/new > >> > end > > >> > Is there a better way that I''m missing?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---