[入門]railsで簡単なヘルパーを作成してみる
( Ruby )viewで使うようのhelperを作成します。
style="display: none"<%end%>>
<%= render @cart %>
これじゃスマートじゃないよね。というかもっと簡素化します。
<%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
<%= render @cart %>
<%end%>
実際のヘルパーは「app/helpers/appliction_helper.rb」に保存されて以下のように実装すると完了です。
module ApplicationHelper
def hidden_div_if(condition, attributes = {}, &block)
if condition
attributes["style"] = "display: none"
end
content_tag("div", attributes, &block)
end
end
