If you know grails, there is a feature like this: <% user.name? %> that prevents to chek if the value is null or not. In rails view I have to do <% if user.name %> <%= user.name %> <% end %> to display user.name if it is not nil. There is a shortcut or some feature like that in grails? -- 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.
user.presence || "" see http://rubydoc.info/docs/rails/3.0.0/Object:presence On Fri, Sep 10, 2010 at 10:25 AM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If you know grails, there is a feature like this: <% user.name? %> > that prevents to chek if the value is null or not. > In rails view I have to do > <% if user.name %> > <%= user.name %> > <% end %> > to display user.name if it is not nil. > There is a shortcut or some feature like that in grails? > > -- > 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.
And if user can potentially be nil, you can also use user.try(:presence) || "" This will avoid "undefined method ''presence'' for nil:NilClass" errors. On 10 Sep 2010, at 10:27, Christiaan Van den Poel wrote:> user.presence || "" > > see http://rubydoc.info/docs/rails/3.0.0/Object:presence > > On Fri, Sep 10, 2010 at 10:25 AM, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > If you know grails, there is a feature like this: <% user.name? %> > that prevents to chek if the value is null or not. > In rails view I have to do > <% if user.name %> > <%= user.name %> > <% end %> > to display user.name if it is not nil. > There is a shortcut or some feature like that in grails?Best regards Peter De Berdt -- 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.
On 10 September 2010 09:25, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If you know grails, there is a feature like this: <% user.name? %> > that prevents to chek if the value is null or not. > In rails view I have to do > <% if user.name %> > <%= user.name %> > <% end %> > to display user.name if it is not nil.Not a general solution I know, but in such a situation I supply a method of User, display_name, that returns the name or empty string (or "Unknown" possibly, dependent on requirement). Colin -- 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.
Msan Msan wrote:> If you know grails, there is a feature like this: <% user.name? %> > that prevents to chek if the value is null or not. > In rails view I have to do > <% if user.name %> > <%= user.name %> > <% end %> > to display user.name if it is not nil. > There is a shortcut or some feature like that in grails?<% if user && user.name %> ... ... -- 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-/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.
On 10 September 2010 09:25, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If you know grails, there is a feature like this: <% user.name? %> > that prevents to chek if the value is null or not. > In rails view I have to do > <% if user.name %> > <%= user.name %> > <% end %> > to display user.name if it is not nil. > There is a shortcut or some feature like that in grails? ><%= user.name if user.name %> may be the most concise, or <%= user.name if user && user.name %> if user may be nil Colin -- 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.