Joel Oliveira
2008-Nov-24 02:31 UTC
calling a partial from within a model / model''s method?
Hey everyone, I have a model I extended w/STI to allow me to write little widgets or modules for my web-app ... that part''s working well. However, I''m getting to the point where, depending on the model''s type/class I would like to render a partial so that each type has it''s own look/feel/content. I found this : http://www.compulsivoco.com/2008/10/rendering-rails-partials-in-a-model-or-background-task/.. and it works pretty well. I had all my models calling their own partials, but, helper functions like link_to and url_for, etc, will not work. As mentioned in the comments to that blog post> Love the tip, *it breaks though when your view code uses helper methods*, > to do this, I stole from Rails internal private method, > initialize_template_class: > > template_instance > ActionView::Base.new(Rails::Configuration.new.view_path) > template_instance.extend ApplicationController.master_helper_module > content = template_instance.render(:partial => self.view_path(self.profile) > + ''/show'', :locals => {:item => self}) >I tried this, but am still not having any luck. Has anyone tried this sort of thing before? I''m trying to do a simple link_to for a nested resource (which works fine within the context of a straight view, but I get this when calling the partial from my model(s) You have a nil object when you didn''t expect it! The error occurred while evaluating nil.url_for Thanks everyone! - Joel --~--~---------~--~----~------------~-------~--~----~ 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 Nov 23, 9:31 pm, "Joel Oliveira" <joel.olive...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey everyone, > > I have a model I extended w/STI to allow me to write little widgets or > modules for my web-app ... that part''s working well. However, I''m getting > to the point where, depending on the model''s type/class I would like to > render a partial so that each type has it''s own look/feel/content. > > I found this :http://www.compulsivoco.com/2008/10/rendering-rails-partials-in-a-mod..... > and it works pretty well. I had all my models calling their own > partials, but, helper functions like link_to and url_for, etc, will not > work. As mentioned in the comments to that blog post > > > Love the tip, *it breaks though when your view code uses helper methods*, > > to do this, I stole from Rails internal private method, > > initialize_template_class: > > > template_instance > > ActionView::Base.new(Rails::Configuration.new.view_path) > > template_instance.extend ApplicationController.master_helper_module > > content = template_instance.render(:partial => self.view_path(self.profile) > > + ''/show'', :locals => {:item => self}) > > I tried this, but am still not having any luck. > > Has anyone tried this sort of thing before? I''m trying to do a simple > link_to for a nested resource (which works fine within the context of a > straight view, but I get this when calling the partial from my model(s) > > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.url_for > > Thanks everyone! > > - JoelHi Joel. I''m not quite sure why you want to render partials or templates from within your model(s). I''m sure you have some good reasons, but sometimes there''re ways of accomplishing what you want without violating the "rules" of the MVC design pattern. Would you mind explaining in more detail why your model(s) need to render partials and/or templates? Cheers, Nick --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Joel Oliveira
2008-Nov-24 13:37 UTC
Re: calling a partial from within a model / model''s method?
Hey Nick! First - thanks for the reply. Second - the reason why I''m trying to do this is to allow whatever type of widget model I''m creating to render its own particular partial, in the context of the "area" a user will place it - header, footer, sidebar, etc. I thought that calling a helper to weed out the objects for a particular "area" and then letting the model call its partial on its own, would be the easier way to do it, despite the normal MVC rules. I know it''s against the rules - so I''m wondering if there''s an easier way? Any input would be appreciated! :) Thanks! - Joel On Sun, Nov 23, 2008 at 9:38 PM, Nick <nick-qGbiljoI0DQkmLvzuZlaBw@public.gmane.org> wrote:> > On Nov 23, 9:31 pm, "Joel Oliveira" <joel.olive...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hey everyone, > > > > *snip* > > > > Thanks everyone! > > > > - Joel > > Hi Joel. I''m not quite sure why you want to render partials or > templates from within your model(s). I''m sure you have some good > reasons, but sometimes there''re ways of accomplishing what you want > without violating the "rules" of the MVC design pattern. > > Would you mind explaining in more detail why your model(s) need to > render partials and/or templates? > > Cheers, > Nick > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Nick Hoffman
2008-Nov-24 16:41 UTC
Re: calling a partial from within a model / model''s method?
On 2008-11-24, at 08:37, Joel Oliveira wrote:> Hey Nick! > > First - thanks for the reply. > > Second - the reason why I''m trying to do this is to allow whatever > type of widget model I''m creating to render its own particular > partial, in the context of the "area" a user will place it - header, > footer, sidebar, etc. I thought that calling a helper to weed out > the objects for a particular "area" and then letting the model call > its partial on its own, would be the easier way to do it, despite > the normal MVC rules. I know it''s against the rules - so I''m > wondering if there''s an easier way? > > Any input would be appreciated! :) > > Thanks! > > - JoelHi Joel. Why not create a helper method that determines which partial should be rendered? For example: http://pastie.org/pastes/322615 -Nick --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Joel Oliveira
2008-Dec-12 02:21 UTC
Re: calling a partial from within a model / model''s method?
Weeks later I finally got around to figuring out how to best tackle this and your suggestion was right on the money. I didn''t want to let this lie without mentioning that the best bet started with that nugget from the pastie link. Thanks Nick :) - Joel On Mon, Nov 24, 2008 at 11:41 AM, Nick Hoffman <nick-qGbiljoI0DQkmLvzuZlaBw@public.gmane.org> wrote:> > On 2008-11-24, at 08:37, Joel Oliveira wrote: > > Hey Nick! > > > > First - thanks for the reply. > > > > Second - the reason why I''m trying to do this is to allow whatever > > type of widget model I''m creating to render its own particular > > partial, in the context of the "area" a user will place it - header, > > footer, sidebar, etc. I thought that calling a helper to weed out > > the objects for a particular "area" and then letting the model call > > its partial on its own, would be the easier way to do it, despite > > the normal MVC rules. I know it''s against the rules - so I''m > > wondering if there''s an easier way? > > > > Any input would be appreciated! :) > > > > Thanks! > > > > - Joel > > Hi Joel. Why not create a helper method that determines which partial > should be rendered? For example: > http://pastie.org/pastes/322615 > > -Nick > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Nick Hoffman
2008-Dec-15 18:15 UTC
Re: calling a partial from within a model / model''s method?
On 2008-12-11, at 21:21, Joel Oliveira wrote:> Weeks later I finally got around to figuring out how to best tackle > this and your suggestion was right on the money. I didn''t want to > let this lie without mentioning that the best bet started with that > nugget from the pastie link. Thanks Nick :) > > - JoelI''m glad to hear it helped! Good luck, mate. -Nick --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---