is this the prefered way to render a special rhtml-file if a Record.find leads to an empty result? def dummy @users = User.find(:all, :conditions => "username = ''dsr321''") if @users.blank? redirect_to :action => "emptyresult" end end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jochen Kaechelin wrote:> is this the prefered way to render a special rhtml-file if a > Record.find leads to an empty result? > > > def dummy > @users = User.find(:all, :conditions => "username = ''dsr321''") > if @users.blank? > redirect_to :action => "emptyresult" > end > endI usually use the same template with some conditional logic to check if the resultset is empty. You could do what you''re suggesting if you want a totally different view. Or you could just choose to render another template (IIRC this should do it, check the API otherwise): render :template => "mycontroller/emptyresult" -- Cheers, - Jacob Atzen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> > Jochen Kaechelin wrote: >> is this the prefered way to render a special rhtml-file if a >> Record.find leads to an empty result? >> >> >> def dummy >> @users = User.find(:all, :conditions => "username = ''dsr321''") >> if @users.blank? >> redirect_to :action => "emptyresult" >> end >> end > > I usually use the same template with some conditional logic to > check if > the resultset is empty. You could do what you''re suggesting if you > want > a totally different view. Or you could just choose to render > another > template (IIRC this should do it, check the API otherwise): > > render :template => "mycontroller/emptyresult"Ok, but is this the prefered syntax: if @users.blank? or should I use something different? When I render the template with an empty result set I get "nil" but "if @users.nil?" does not work. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 7/23/07, Jacob Atzen <jacob-4U2y0bnePT5NzRJJ8cAMrg@public.gmane.org> wrote:> > Jochen Kaechelin wrote: > > is this the prefered way to render a special rhtml-file if a > > Record.find leads to an empty result? > > > > > > def dummy > > @users = User.find(:all, :conditions => "username = ''dsr321''") > > if @users.blank? > > redirect_to :action => "emptyresult" > > end > > end > > I usually use the same template with some conditional logic to check if > the resultset is empty. You could do what you''re suggesting if you want > a totally different view. Or you could just choose to render another > template (IIRC this should do it, check the API otherwise): > > render :template => "mycontroller/emptyresult"The advice from http://therailsway.com/ is to avoid conditional logic in views with more than one branch. It''s OK to say "display x if y" but it''s not OK to say "if x, display y, else display z". Based on that, I''d avoid having this decision in the template itself. I''m not sure what the conventional wisdom is re: whether to redirect to a different action or simply render a different template, but I would (personally) favor rendering the different template unless there was additional preparation necessary which might cloud the meaning of the first action. David> > -- > Cheers, > - Jacob Atzen > > > >--~--~---------~--~----~------------~-------~--~----~ 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 7/23/07, Jochen Kaechelin <gissmoh-Vg5pTm5GbeLoK6nBLMlh1Q@public.gmane.org> wrote:> > > > > Jochen Kaechelin wrote: > >> is this the prefered way to render a special rhtml-file if a > >> Record.find leads to an empty result? > >> > >> > >> def dummy > >> @users = User.find(:all, :conditions => "username = ''dsr321''") > >> if @users.blank? > >> redirect_to :action => "emptyresult" > >> end > >> end > > > > I usually use the same template with some conditional logic to > > check if > > the resultset is empty. You could do what you''re suggesting if you > > want > > a totally different view. Or you could just choose to render > > another > > template (IIRC this should do it, check the API otherwise): > > > > render :template => "mycontroller/emptyresult" > > Ok, but is this the prefered syntax: > > if @users.blank? > > or should I use something different? > > When I render the template with an empty result set I get "nil" but > "if @users.nil?" does not work.Assuming you''re doing something like @users = User.find(...), you should have an empty array. If you''re doing something different please post it. Otherwise I''d try @users.empty? Also - you shouldn''t be getting nil from @users. Are you explicitly referencing one of them in the view? Like this: @users[0]?> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 7/23/07, Jochen Kaechelin <gissmoh-Vg5pTm5GbeLoK6nBLMlh1Q@public.gmane.org> wrote: >> >> > >> > Jochen Kaechelin wrote: >> >> is this the prefered way to render a special rhtml-file if a >> >> Record.find leads to an empty result? >> >> >> >> >> >> def dummy >> >> @users = User.find(:all, :conditions => "username >> ''dsr321''") >> >> if @users.blank? >> >> redirect_to :action => "emptyresult" >> >> end >> >> end >> > >> > I usually use the same template with some conditional logic to >> > check if >> > the resultset is empty. You could do what you''re suggesting if >> you >> > want >> > a totally different view. Or you could just choose to render >> > another >> > template (IIRC this should do it, check the API otherwise): >> > >> > render :template => "mycontroller/emptyresult" >> >> Ok, but is this the prefered syntax: >> >> if @users.blank? >> >> or should I use something different? >> >> When I render the template with an empty result set I get "nil" >> but >> "if @users.nil?" does not work. > > Assuming you''re doing something like @users = User.find(...), you > should have an empty array.I use this one: unless @users.empty? redirect_to :action => "list" else redirect_to :action => "fehler" end and everything works fine.> If you''re doing something different > please post it. Otherwise I''d try @users.empty?> Also - you shouldn''t be getting nil from @users. Are you > explicitly referencing one of them in the view? Like > this: @users[0]?No, I make something like: @users.each do |u| .... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David Chelimsky wrote:> On 7/23/07, Jacob Atzen <jacob-4U2y0bnePT5NzRJJ8cAMrg@public.gmane.org> wrote: >> Jochen Kaechelin wrote: >>> is this the prefered way to render a special rhtml-file if a >>> Record.find leads to an empty result? >>> >>> >>> def dummy >>> @users = User.find(:all, :conditions => "username = ''dsr321''") >>> if @users.blank? >>> redirect_to :action => "emptyresult" >>> end >>> end >> I usually use the same template with some conditional logic to check if >> the resultset is empty. You could do what you''re suggesting if you want >> a totally different view. Or you could just choose to render another >> template (IIRC this should do it, check the API otherwise): >> >> render :template => "mycontroller/emptyresult" > > The advice from http://therailsway.com/ is to avoid conditional logic > in views with more than one branch. It''s OK to say "display x if y" > but it''s not OK to say "if x, display y, else display z". Based on > that, I''d avoid having this decision in the template itself.Move it into a helper then.> I''m not sure what the conventional wisdom is re: whether to redirect > to a different action or simply render a different template, but I > would (personally) favor rendering the different template unless there > was additional preparation necessary which might cloud the meaning of > the first action.I agree that render is better than redirect to avoid the extra http request. But note that the idea of having different templates / actions based on the number of results in a resultset breaks down as soon as the number of resultsets displayed grows due to the combinatorial explosion. That is if you wants to display both users and posts you will have to create 4 templates, add another variable and this number grows to 8 - not to mention the logic in the controller to handle this. -- Cheers, - Jacob Atzen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---