長くてみにくかったviewの記述をapplication_helper.rb
に移動させてcontent_tag
と言うものを使ってhtmlタグを生成する
content_tag
について
参考: https://qiita.com/yusaku_/items/d6e98b77124131e628fb
rails/app/helpers/try_helper.rb
def discount_text(service, provider)
discount_price = provider.key == "noe-jx-group" ? service['discount'].to_f : service['discount'].to_i
discount_from_energy = if SetPlan.is_discount_from_energy?(service)
"/kWh"
elsif SetPlan.yearly?(service)
"/年"
elsif SetPlan.monthly?(service)
"/月"
end
content_tag(:div, class: "e__saving") do
content_tag(:span, "割引額", class: "e__tag") + content_tag(:span, discount_price, class: "e__number e__deals" ) + "円" + discount_from_energy
end
end
rails/app/views/try/_set_discount.html.erb
// 修正前
<div class="e__saving"><span class="e__tag">割引額</span><span class="e__number e__deals"><%= service['discount'].to_i %></span>円<% if SetPlan.is_discount_from_energy?(service) %>/kWh<% elsif SetPlan.monthly?(service) %>/月<% elsif SetPlan.yearly?(service) %>/年<% end %></div>
// 修正後(とっても短くなった!)
<%= discount_text(service, @provider) %>