Forum: Ruby on Rails Rails Way to display if !blank?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A6d9ea53328a9e547cd10d4c30586bdb?d=identicon&s=25 Robert Head (roberthead)
on 2007-06-01 01:18
I find myself doing this all the time:

<% if !foo.blank? %>
  <div class="foo_class"><%= foo %></div>
<% end %>

Is there a better way?

Many thanks,
Rob
A7f6b372a89349258d08a4d7acb186d4?d=identicon&s=25 Serengeti (Guest)
on 2007-06-01 01:45
(Received via mailing list)
I know that in general it is a "good idea" to move logic out of
templates and into helpers when possible.

For example, you could create a helper (either in the particular
controller's helper file or in the application's helper file).  Your
helper might be called "show_div" and it might accept the thing you
want to display as a parameter.

The helper would then test whether or not the object is empty and
display your output when the object is not empty.

Let me know if that is not enough detail.  I can provide a working
example.

In any case, that's what I have been doing in cases like this.  There
may be other ways too.  I'm looking forward to seeing what others
suggest.

On May 31, 6:18 pm, Robert Head <rails-mailing-l...@andreas-s.net>
363548d0cfaaa4bab88747f31ad49c02?d=identicon&s=25 Shane Vitarana (Guest)
on 2007-06-01 02:08
(Received via mailing list)
Using unless instead of if !foo might make it easier on the eyes.

<% unless foo.blank? %>
 <div class="foo_class"><%= foo %></div>
<% end %>

Shane

On 5/31/07, Robert Head <rails-mailing-list@andreas-s.net> wrote:
> Rob
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
http://shanesbrain.net
C237cf537a06b60921c97804679e3b15?d=identicon&s=25 John Barnette (Guest)
on 2007-06-01 03:20
(Received via mailing list)
...and using content_tag (or elemental:
http://blog.dangosaur.us/elemental/) will let you golf it down to a
one-liner if you want.

<%= div(foo, :class => :foo_class) unless foo.blank? %>


~ j.
5190330ad8f1b06d35e2c2da73dc623c?d=identicon&s=25 Eric Anderson (Guest)
on 2007-06-01 15:20
(Received via mailing list)
Robert Head wrote:
> <% if !foo.blank? %>
>   <div class="foo_class"><%= foo %></div>
> <% end %>

The elemental they mentioned in a previous message looks great. I have
also found this helper useful.

# Will print the content in the given tag if the condition is true. If
# no condition is provided then it will use the existance of real data
# in the content value to determine if content should be printed
def if_tag(tag_name, content, html_options={},condition=!content.blank?)
   content_tag(tag_name, content, html_options) if condition
end

I use would reproduce what you were trying to do with:

<%=if_tag('div', foo, :class=>'foo_class')%>

Eric
8608f2d7f02c6d0f1c349b7b94b1cea5?d=identicon&s=25 liquidautumn (Guest)
on 2007-06-07 17:50
(Received via mailing list)
there is even more elegant solution using markaby.
by default markaby rendering empty tags, but you would easily change
its behaviour, look into builder.rb, search for tag! method and amend
it to return nothing if passed block converted to string is empty.
then all you need to write in in template is

div.foo_class 'foo'
This topic is locked and can not be replied to.