LoginSignup
2
3

More than 5 years have passed since last update.

ちょっと便利な content_tag_if ヘルパー

Last updated at Posted at 2012-02-12

いまいちありがたい感じがしない content_tag ヘルパーですが、ちょっと拡張してみると結構便利になりました。

def content_tag_if(boolean, name, content_or_options_with_block = nil, options = nil, escape = true, &block)
  if boolean
    content_tag(name, content_or_options_with_block, options, escape, &block)
  end
end

こういうヘルパーを作成しておくと、

<% if boolean %>
  <div>hoge</div>
<% end %>

と書いていたものが、

<%= content_tag_if boolean, :div, 'hoge' %>

とすっきり書けます。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3