0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

フリマアプリにて桁区切りの実装

Last updated at Posted at 2020-08-15

フリマアプリ開発にて実装した桁区切りのまとめを記載していきます。

ruby"2.5.1"
Rails"5.2.4.3"

価格が大きくなってくると金額が分かりにくくなるため、下記の3ページの商品価格について桁区切りを実装していきます。
今回はdelimitedというヘルパーメソッドを使用します。

<変更前>

  • 修正前のトップページ画面
スクリーンショット 2020-08-14 10.48.27.png
  • 修正前の商品詳細ページ画面
スクリーンショット 2020-08-14 10.49.04.png
  • 修正前の商品購入ページ画面
スクリーンショット 2020-08-14 10.49.34.png

<手順>

①「helpers/products_helper.rb」に下記の通りにヘルパーメソッドを定義

 (※商品詳細・購入ページ用)

products_helper.rb
     module ProductsHelper
       def converting_to_jpy(price)
    	 "#{price.to_s(:delimited, delimiter: ',')}円"
       end
     end
②「helpers/tops_helper.rb」に下記の通りにヘルパーメソッドを定義

 (※トップページ用)

tops_helper.rb
     module TopsHelper
       def converting_to_jpy_top(price)
         "#{price.to_s(:delimited, delimiter: ',')}"
       end
     end
③「products/show.html.haml」のpriceの箇所の表記を変更
show.html.haml
     .show__main__product__content__information__price
       %span
         = converting_to_jpy(@product.price)
④ 「products/purchase.html.haml」のpriceの箇所の表記を変更
purchase.html.haml
     %p.purchase__main__product-info__inner__content__detail__price
       %span
        = converting_to_jpy(@product.price)
⑤「_item-preview.html.haml」のpriceの箇所の表記を変更
_item-preview.html.haml
     .item__caption__details__price
       = converting_to_jpy_top(product.price)

※productと分けるためにconverting_to_jpy_topとしています。

これで各ページの変更は完了。

<変更後>

  • 修正後のトップページ
    スクリーンショット 2020-08-14 15.18.46.png

  • 修正後の商品詳細ページ
    スクリーンショット 2020-08-14 15.19.08.png

  • 修正後の商品購入ページ
    スクリーンショット 2020-08-14 15.19.26.png

桁区切りを導入したことで、多少ページに締まりが出てきたかなと思いました。

以上です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?