LoginSignup
0
3

More than 1 year has passed since last update.

Rails 消費税計算

Posted at

プログラミングの学習を始めて3ヶ月、
Ruby on Railsでの、消費税計算についてまとめます。

前提として、
商品(Product)とカート(Cart)の作成が終えていて、
Productにはsales_priseのカラムがあります。

モデルに記載

Productモデルに記載します。

Product.rb
def add_tax_sales_price
  (self.sales_price * 1.10).round
end

Productのカラムである sales_price の前に
必ず add_tax_ を記載してください。

私はそれで rails s した時に永遠にループしてしまい
serverが起動せず大変な目にあいました。

viewに記載

消費税を計算して反映させたい部分に

<%= @product.add_tax_sales_price.to_s(:dalimited) %>

Productモデルに * 1.10 (2021年6月現在)の税率のため、
変更があった場合はmodelを変更するだけでviewページは反映されます。

(:dalimited)は数値を3桁区切りにしてくれます。
例: 12,345,678
1,234円

0
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
0
3