I find myself doing the following quite a lot: <% if user %><%= user.name %><% else %>Anonymous<% end %> I know that if I dont care about an else I can just do: <%= user.name if user %> Is there any simpler way to handle the else? This is a simple example so it''s not really an issue.. but for more complicated tests this gets quite tedious. -- Posted via http://www.ruby-forum.com/.
On Apr 8, 2006, at 3:32 PM, The Barge wrote:> I find myself doing the following quite a lot: > > <% if user %><%= user.name %><% else %>Anonymous<% end %> > > I know that if I dont care about an else I can just do: > > <%= user.name if user %> > > Is there any simpler way to handle the else? This is a simple example > so it''s not really an issue.. but for more complicated tests this gets > quite tedious. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsYou can use ternary conditionals. <%= user ? user.name : "Anonymous" %> Cheers- -Ezra
Brilliant! I didn''t even think of that! I''ve been doing it for ages in Java/JSP. Not sure why it didn''t occur to me. Ezra Zygmuntowicz wrote:> You can use ternary conditionals. > > <%= user ? user.name : "Anonymous" %>-- Posted via http://www.ruby-forum.com/.
The Barge wrote:> Brilliant! I didn''t even think of that! I''ve been doing it for ages in > Java/JSP. Not sure why it didn''t occur to me. > > Ezra Zygmuntowicz wrote: >> You can use ternary conditionals. >> >> <%= user ? user.name : "Anonymous" %><%= user.name||"Anonymous" %> joey__ now j`ey on IRC -- Posted via http://www.ruby-forum.com/.
Hi -- On Sun, 9 Apr 2006, joey__ wrote:> The Barge wrote: >> Brilliant! I didn''t even think of that! I''ve been doing it for ages in >> Java/JSP. Not sure why it didn''t occur to me. >> >> Ezra Zygmuntowicz wrote: >>> You can use ternary conditionals. >>> >>> <%= user ? user.name : "Anonymous" %> > > <%= user.name||"Anonymous" %>The problem with that is that if user is undefined or nil, it will raise an error when it tries to call user.name. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" coming in PDF April 15, and in paper May 1! http://www.manning.com/black
> I find myself doing the following quite a lot: > > <% if user %><%= user.name %><% else %>Anonymous<% end %> > > Is there any simpler way to handle the else? This is a simple example > so it''s not really an issue.. but for more complicated tests this gets > quite tedious.For me, I moved the whole thing into a helper. Because usernames are a potential relationship that I may want to give more information on, I created a helper that did this, parameterized by the user object. In the simple case, it does what you show. In other cases it will return a link to the user''s information. If the user is the favorite of the current user, it may throw in a span tag so that I can colour the link differently, or include a small inline image. In my view, I''ll have something like: <%= render_username post.author %> Keeps the view clean, and still provides the flexibility I want, plus it keeps things nice and DRY. Brad -- Bradley Mazurek