Suppose that I want to have a block of default content within a template that is replaced with the content of a specific partial only if that partial exists. Obviously there is the brute force approach of simply rendering the default content unless the relevant partial file exists in which case the relevant partial is rendered instead. I suspect that there may be a more elegant way to skin this cat. Any suggestions? Thanks for any input. ... doug -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Doug, This may actually be worse but it is another way... <% begin %> <%= render :partial => ''custom'' %> <% rescue ActionView::MissingTemplate %> Default <% end %> Anthony Crumley http://commonthread.com On Fri, Apr 9, 2010 at 7:29 PM, doug <ddjolley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Suppose that I want to have a block of default content within a > template that is replaced with the content of a specific partial only > if that partial exists. Obviously there is the brute force approach > of simply rendering the default content unless the relevant partial > file exists in which case the relevant partial is rendered instead. I > suspect that there may be a more elegant way to skin this cat. Any > suggestions? > > Thanks for any input. > > ... doug > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Anthony, <% begin %> <%= render :partial => ''custom'' %> <% rescue ActionView::MissingTemplate %> Default <% end %> why this begin and end are used what is mean by <% rescue ActionView::MissingTemplate %> can you tell me On Apr 10, 11:19 am, Anthony Crumley <anthony.crum...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Doug, > > This may actually be worse but it is another way... > > <% begin %> > <%= render :partial => ''custom'' %> > <% rescue ActionView::MissingTemplate %> > Default > <% end %> > > Anthony Crumleyhttp://commonthread.com > > On Fri, Apr 9, 2010 at 7:29 PM, doug <ddjol...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Suppose that I want to have a block of default content within a > > template that is replaced with the content of a specific partial only > > if that partial exists. Obviously there is the brute force approach > > of simply rendering the default content unless the relevant partial > > file exists in which case the relevant partial is rendered instead. I > > suspect that there may be a more elegant way to skin this cat. Any > > suggestions? > > > Thanks for any input. > > > ... doug > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Rahul, Exceptions handling in Ruby is done the following way. begin <some code> rescue <exception> <some exception handling code> end When a partial template is missing Rails will throw an exception. As a result, to catch a missing partial template exception (ActionView::MissingTemplate) you need to rescue that exception by name. In ERB the syntax for this is <% rescue ActionView::MissingTemplate %>. Anthony Crumley http://commonthread.com On Sat, Apr 10, 2010 at 4:05 AM, Rahul Mehta <rahul23134654-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Hi Anthony, > > <% begin %> > <%= render :partial => ''custom'' %> > <% rescue ActionView::MissingTemplate %> > Default > <% end %> > > why this begin and end are used > what is mean by > <% rescue ActionView::MissingTemplate %> > > can you tell me > > > On Apr 10, 11:19 am, Anthony Crumley <anthony.crum...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > Doug, > > > > This may actually be worse but it is another way... > > > > <% begin %> > > <%= render :partial => ''custom'' %> > > <% rescue ActionView::MissingTemplate %> > > Default > > <% end %> > > > > Anthony Crumleyhttp://commonthread.com > > > > On Fri, Apr 9, 2010 at 7:29 PM, doug <ddjol...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Suppose that I want to have a block of default content within a > > > template that is replaced with the content of a specific partial only > > > if that partial exists. Obviously there is the brute force approach > > > of simply rendering the default content unless the relevant partial > > > file exists in which case the relevant partial is rendered instead. I > > > suspect that there may be a more elegant way to skin this cat. Any > > > suggestions? > > > > > Thanks for any input. > > > > > ... doug > > > > > -- > > > 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<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > <rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%252Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> This may actually be worse but it is another way...In terms of elegance, I think that it''s about a toss up. However, I think that your suggestion has a slight distinct advantage. If I use my approach, I have to deal with the actual partial twice, once to test whether it exists and once to render it if it does. Using your approach, I only have to deal with the actual partial once, i.e., to render it. I''m adopting your approach whch, btw, works great. Thanks for the suggestion. ... doug -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Anthony , Thanks for reply, its good to know that exception handling in rails is done like this. thanks Rahul On Apr 10, 2:05 pm, Rahul Mehta <rahul23134...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Anthony, > > <% begin %> > <%= render :partial => ''custom'' %> > <% rescue ActionView::MissingTemplate %> > Default > <% end %> > > why this begin and end are used > what is mean by > <% rescue ActionView::MissingTemplate %> > > can you tell me > > On Apr 10, 11:19 am, Anthony Crumley <anthony.crum...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > Doug, > > > This may actually be worse but it is another way... > > > <% begin %> > > <%= render :partial => ''custom'' %> > > <% rescue ActionView::MissingTemplate %> > > Default > > <% end %> > > > Anthony Crumleyhttp://commonthread.com > > > On Fri, Apr 9, 2010 at 7:29 PM, doug <ddjol...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Suppose that I want to have a block of default content within a > > > template that is replaced with the content of a specific partial only > > > if that partial exists. Obviously there is the brute force approach > > > of simply rendering the default content unless the relevant partial > > > file exists in which case the relevant partial is rendered instead. I > > > suspect that there may be a more elegant way to skin this cat. Any > > > suggestions? > > > > Thanks for any input. > > > > ... doug > > > > -- > > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.