0
0

More than 3 years have passed since last update.

application_helperを使う

Posted at

長くてみにくかった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) %>
0
0
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
0
0