This seems like it must be a common question, so I was surprised not to see it in any of the Components Howtos or Articles. (Or at least, if it was there, I missed it.) I have just started using Components to get a little more increased modularity from my code. They''re great. Exactly what I needed. However, they don''t interact well with layouts. Every time I call a Component, it is rendered with all of the header/sidebar stuff that''s in the application layout (application.rb). My first thought was to create a separate action called (for example) "index_component" that just called "index", and to put "layout :except index_component". But this seemed a slight violoation of the DRY philosophy, and updating my layout line seemed like it would be a pain. (I suppose I could use a regex for anything ending with "_component", but I don''t particularly like that either. What I really want is a way to find out that the action I''m processing is from a component call. Is there a way to do this. This allows me to write "layout :except not_in_component", which exactly solves my problem. I have implemented this myself with the following code: ## # Add in the :in_component option to the params so that my controllers # will be able to tell when they''re processing a component. def render_component_as_string(options) options[:params] = (options[:params] || {}).merge({ :in_component => "true" }); super(options) end ## # Use this as the layout value to skip the layout when processing a # component. # Note: will need to change if any controller ever uses its own # layout and not the "application" layout def not_in_component @params.include?(:in_component) ? nil : "application" end And this seems to work just great for my purposes. Anyway, I''m wondering if there''s a Rails-approved technique for doing what I''m doing, and if not, perhaps this is a technique that others would find useful. Thanks for your time. George
This seems like it must be a common question, so I was surprised not to see it in any of the Components Howtos or Articles. (Or at least, if it was there, I missed it.) I have just started using Components to get a little more increased modularity from my code. They''re great. Exactly what I needed. However, they don''t interact well with layouts. Every time I call a Component, it is rendered with all of the header/sidebar stuff that''s in the application layout (application.rb). My first thought was to create a separate action called (for example) "index_component" that just called "index", and to put "layout :except index_component". But this seemed a slight violoation of the DRY philosophy, and updating my layout line seemed like it would be a pain. (I suppose I could use a regex for anything ending with "_component", but I don''t particularly like that either. What I really want is a way to find out that the action I''m processing is from a component call. Is there a way to do this. This allows me to write "layout :except not_in_component", which exactly solves my problem. I have implemented this myself with the following code: ## # Add in the :in_component option to the params so that my controllers # will be able to tell when they''re processing a component. def render_component_as_string(options) options[:params] = (options[:params] || {}).merge({ :in_component => "true" }); super(options) end ## # Use this as the layout value to skip the layout when processing a # component. # Note: will need to change if any controller ever uses its own # layout and not the "application" layout def not_in_component @params.include?(:in_component) ? nil : "application" end And this seems to work just great for my purposes. Anyway, I''m wondering if there''s a Rails-approved technique for doing what I''m doing, and if not, perhaps this is a technique that others would find useful. Thanks for your time. George
[this may be a repost. I''m having minor problems getting my email through to this list. I apologize if you receive it twice.] This seems like it must be a common question, so I was surprised not to see it in any of the Components Howtos or Articles. (Or at least, if it was there, I missed it.) I have just started using Components to get a little more increased modularity from my code. They''re great. Exactly what I needed. However, they don''t interact well with layouts. Every time I call a Component, it is rendered with all of the header/sidebar stuff that''s in the application layout (application.rb). My first thought was to create a separate action called (for example) "index_component" that just called "index", and to put "layout :except index_component". But this seemed a slight violoation of the DRY philosophy, and updating my layout line seemed like it would be a pain. (I suppose I could use a regex for anything ending with "_component", but I don''t particularly like that either. What I really want is a way to find out that the action I''m processing is from a component call. Is there a way to do this. This allows me to write "layout :except not_in_component", which exactly solves my problem. I have implemented this myself with the following code: ## # Add in the :in_component option to the params so that my controllers # will be able to tell when they''re processing a component. def render_component_as_string(options) options[:params] = (options[:params] || {}).merge({ :in_component => "true" }); super(options) end ## # Use this as the layout value to skip the layout when processing a # component. # Note: will need to change if any controller ever uses its own # layout and not the "application" layout def not_in_component @params.include?(:in_component) ? nil : "application" end And this seems to work just great for my purposes. Anyway, I''m wondering if there''s a Rails-approved technique for doing what I''m doing, and if not, perhaps this is a technique that others would find useful. Thanks for your time. George
> Anyway, I''m wondering if there''s a Rails-approved technique for doing > what I''m doing, and if not, perhaps this is a technique that others > would find useful.Put the ''uses_component_template_root'' method in your component controller class. This uses the layout in /components/layouts instead. http://manuals.rubyonrails.com/read/chapter/73 rick -- rick http://techno-weenie.net
Okay, I''ve read that again, and I''m not sure it solves my problem. I''m trying to use the same template files to respond to both a user action and a component call. (Either display the ToDo list as the only thing on the page, or as a component in the sidebar, for example.) Having a simple way to distinguish between a component call an action call lets me do this. If I understand what I''ve read correctly, ''users_component_template_root'' is a class method that I should call in my class definition. Doesn''t this imply using a controller to process user actions and using it to handle components are mutually exclusive? Should I have a ModelController AND a ModelComponentController? If so, should call call the component from the base controller in order to avoid the dreaded "RY"? Hmm, I wonder why all my AJAX actions don''t have the same problem. Does calling "render_partial" directly avoid all of the layout stuff? Thanks again, George On 4/29/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Anyway, I''m wondering if there''s a Rails-approved technique for doing > > what I''m doing, and if not, perhaps this is a technique that others > > would find useful. > > Put the ''uses_component_template_root'' method in your component > controller class. This uses the layout in /components/layouts > instead. > > http://manuals.rubyonrails.com/read/chapter/73 > > rick > > -- > rick > http://techno-weenie.net >
On 4/29/05, George Madrid <gmadrid-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Okay, I''ve read that again, and I''m not sure it solves my problem. > > I''m trying to use the same template files to respond to both a user > action and a component call. (Either display the ToDo list as the only > thing on the page, or as a component in the sidebar, for example.) > Having a simple way to distinguish between a component call an action > call lets me do this. > > If I understand what I''ve read correctly, > ''users_component_template_root'' is a class method that I should call > in my class definition. Doesn''t this imply using a controller to > process user actions and using it to handle components are mutually > exclusive? Should I have a ModelController AND a > ModelComponentController? If so, should call call the component from > the base controller in order to avoid the dreaded "RY"? > > Hmm, I wonder why all my AJAX actions don''t have the same problem. > Does calling "render_partial" directly avoid all of the layout stuff? > > Thanks again, > > GeorgeAh, I misunderstood. I do believe render_partial bypasses the layout. A duplicate action that renders_partial isn''t a great idea. I wonder if you could use method_missing for this? When you call render_component, pass the action ''recent_items_as_component.'' Since you have no action for that, mssing_method is called, strips the _as_component, calls the action, and renders a partial instead? -- rick http://techno-weenie.net
Hello, Just my two cents comment: shouldn''t you just have your ModelComponentController and make the same call to render_component in both your user result page and your page-with-sidebar ? Regards Julien -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of George Madrid Sent: Friday, April 29, 2005 5:02 PM To: Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Components and layouts Okay, I''ve read that again, and I''m not sure it solves my problem. I''m trying to use the same template files to respond to both a user action and a component call. (Either display the ToDo list as the only thing on the page, or as a component in the sidebar, for example.) Having a simple way to distinguish between a component call an action call lets me do this. If I understand what I''ve read correctly, ''users_component_template_root'' is a class method that I should call in my class definition. Doesn''t this imply using a controller to process user actions and using it to handle components are mutually exclusive? Should I have a ModelController AND a ModelComponentController? If so, should call call the component from the base controller in order to avoid the dreaded "RY"? Hmm, I wonder why all my AJAX actions don''t have the same problem. Does calling "render_partial" directly avoid all of the layout stuff? Thanks again, George On 4/29/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Anyway, I''m wondering if there''s a Rails-approved technique for > > doing what I''m doing, and if not, perhaps this is a technique that > > others would find useful. > > Put the ''uses_component_template_root'' method in your component > controller class. This uses the layout in /components/layouts > instead. > > http://manuals.rubyonrails.com/read/chapter/73 > > rick > > -- > rick > http://techno-weenie.net >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails