Conditional wrapper tag helper
It wraps given block into the tag when condition is true, and returns unwrapped block otherwise.
def conditional_tag(name, condition, options=nil, &block)
if condition
content_tag name, capture(&block), options
else
capture(&block)
end
end
Usage:
<%= conditional_tag :div, controller_name == "home", class: "above-fancy" do %>
<header class="site-header">
<%= primary_navigation %>
</header>
<% end -%>
It wraps <header>
in <div class="above-fancy">
only when controller_name == "home"
is true
Written by Andrew Volozhanin
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#