Aaron Wieberg
2007-May-03 17:57 UTC
rendering a different view depending on model attributes
Is there a good way to render a different view based on a models attributes? For instance, say there is a model called Form, which belongs to an Office. And there are 2 offices, say Pre and Post, each one using a different view, or sharing a view. So say we have the views form/show.pre.rhtml form/show.post.rhtml form/comments.rhtml So each office has a specific show view, but share a comments view. Rather than code something that chooses which view to use inside each action, I''d like that to be done automatically. So say at a later date I decide to split the comments view separately for each office, I can just add a new view for each office and it picks the correct one. Thanks for any help! Aaron -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Russell Norris
2007-May-03 18:01 UTC
Re: rendering a different view depending on model attributes
Would if @form.office == "Pre" render :template => "form/show_pre" else render :template => "form/show_post" end not work? Obviously, youd have to edit those view names to fit there. [Not sure that "form/show.pre" wouldn''t have unexpected consequences.] RSL On 5/3/07, Aaron Wieberg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Is there a good way to render a different view based on a models > attributes? For instance, say there is a model called Form, which > belongs to an Office. And there are 2 offices, say Pre and Post, each > one using a different view, or sharing a view. So say we have the views > > form/show.pre.rhtml > form/show.post.rhtml > form/comments.rhtml > > So each office has a specific show view, but share a comments view. > Rather than code something that chooses which view to use inside each > action, I''d like that to be done automatically. So say at a later date > I decide to split the comments view separately for each office, I can > just add a new view for each office and it picks the correct one. > Thanks for any help! > > Aaron > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Aaron Wieberg
2007-May-03 18:28 UTC
Re: rendering a different view depending on model attributes
Russell Norris wrote:> Would > > if @form.office == "Pre" > render :template => "form/show_pre" > else > render :template => "form/show_post" > end > > not work? Obviously, youd have to edit those view names to fit there. > [Not > sure that "form/show.pre" wouldn''t have unexpected consequences.] > > RSLI could do that, I was just trying to avoid that because I could see myself repeating myself a lot. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Russell Norris
2007-May-03 18:32 UTC
Re: rendering a different view depending on model attributes
Ah. perhaps write it out into a method and just call the method from the appropriate places. I wish you could use after_filters to do renders. RSL On 5/3/07, Aaron Wieberg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Russell Norris wrote: > > Would > > > > if @form.office == "Pre" > > render :template => "form/show_pre" > > else > > render :template => "form/show_post" > > end > > > > not work? Obviously, youd have to edit those view names to fit there. > > [Not > > sure that "form/show.pre" wouldn''t have unexpected consequences.] > > > > RSL > > I could do that, I was just trying to avoid that because I could see > myself repeating myself a lot. > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---