質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Q&A

解決済

2回答

7750閲覧

半角数字のみのバリデーションを設定したい

gomappi

総合スコア7

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

0グッド

2クリップ

投稿2021/01/30 13:36

前提・実現したいこと

半角数字のみで保存できるようにしたい

発生している問題・エラーメッセージ

半角数字のみでバリデーションを行っているつもりですが、半角英字が入っても保存されてしまいます。 (実際には保存された値はデータ上ちゃんと数字になっているのですが、数字の後に英字をつけても保存されてしまいます。例:300dollors)

該当のソースコード

itemモデル

ruby

1class Item < ApplicationRecord 2 extend ActiveHash::Associations::ActiveRecordExtensions 3 belongs_to :category 4 belongs_to :condition 5 belongs_to :shipping_fee 6 belongs_to :prefecture 7 belongs_to :send_day 8 belongs_to :user, optional: true 9 has_one :buy 10 has_one_attached :image 11 12 with_options presence: true do 13 validates :image 14 validates :name 15 validates :info 16 validates :price, inclusion: { in: 300..9_999_999 }, format: { with: /\A[0-9]+\z/ } 17 end 18 with_options numericality: { other_than: 1 } do 19 validates :category_id 20 validates :condition_id 21 validates :shipping_fee_id 22 validates :prefecture_id 23 validates :send_day_id 24 end 25end 26
item_spec.rb("priceが半角英数混合では登録できないこと"で登録されてしまいます)

ruby

1require 'rails_helper' 2 3RSpec.describe Item, type: :model do 4 before do 5 @item = FactoryBot.build(:item) 6 end 7 8 describe '商品出品機能' do 9 context '商品出品できるとき' do 10 it 'image,name,info,category_id,condition_id,shipping_fee_id,prefecture_id,send_day_id,priceがあれば出品できる' do 11 expect(@item).to be_valid 12 end 13 it 'priceが300円なら出品できる' do 14 @item.price = 300 15 expect(@item).to be_valid 16 end 17 it 'priceが9,999,999円なら出品できる' do 18 @item.price = 9_999_999 19 expect(@item).to be_valid 20 end 21 it 'priceが半角なら出品できる' do 22 @item.price = 300 23 expect(@item).to be_valid 24 end 25 end 26 context '商品出品できないとき' do 27 it 'imageがないと出品できない' do 28 @item.image = nil 29 @item.valid? 30 expect(@item.errors.full_messages).to include("Image can't be blank") 31 end 32 it 'nameがないと出品できない' do 33 @item.name = '' 34 @item.valid? 35 expect(@item.errors.full_messages).to include("Name can't be blank") 36 end 37 it 'infoがないと出品できない' do 38 @item.info = '' 39 @item.valid? 40 expect(@item.errors.full_messages).to include("Info can't be blank") 41 end 42 it 'category_idがないと出品できない' do 43 @item.category_id = 1 44 @item.valid? 45 expect(@item.errors.full_messages).to include('Category must be other than 1') 46 end 47 it 'condition_idがないと出品できない' do 48 @item.condition_id = 1 49 @item.valid? 50 expect(@item.errors.full_messages).to include('Condition must be other than 1') 51 end 52 it 'shipping_fee_idがないと出品できない' do 53 @item.shipping_fee_id = 1 54 @item.valid? 55 expect(@item.errors.full_messages).to include('Shipping fee must be other than 1') 56 end 57 it 'prefecture_idがないと出品できない' do 58 @item.prefecture_id = 1 59 @item.valid? 60 expect(@item.errors.full_messages).to include('Prefecture must be other than 1') 61 end 62 it 'send_day_idがないと出品できない' do 63 @item.send_day_id = 1 64 @item.valid? 65 expect(@item.errors.full_messages).to include('Send day must be other than 1') 66 end 67 it 'priceがないと出品できない' do 68 @item.price = '' 69 @item.valid? 70 expect(@item.errors.full_messages).to include("Price can't be blank") 71 end 72 it 'priceが300円以下だと出品できない' do 73 @item.price = 299 74 @item.valid? 75 expect(@item.errors.full_messages).to include('Price is not included in the list') 76 end 77 it 'priceが9,999,999円以上だと出品できない' do 78 @item.price = 10_000_000 79 @item.valid? 80 expect(@item.errors.full_messages).to include('Price is not included in the list') 81 end 82 it 'priceが半角数字でないと出品できない' do 83 @item.price = '300' 84 @item.valid? 85 expect(@item.errors.full_messages).to include('Price is not included in the list') 86 end 87 it "priceが半角英数混合では登録できないこと" do 88 @item.price = "300dollars" 89 @item.valid? 90 expect(@item.errors.full_messages).to include("Price is not included in the list") 91 end 92 it "priceが半角英語だけでは登録できないこと" do 93 @item.price = "threemillion" 94 @item.valid? 95 expect(@item.errors.full_messages).to include("Price is not included in the list") 96 end 97 end 98 end 99end

試したこと

こちらのサイトを参考に全ての数値のバリデーションを行いました。

https://gist.github.com/nashirox/38323d5b51063ede1d41

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

解決方法

priceバリデーションにnumericalityをかける。

ruby

1class Item < ApplicationRecord 2 extend ActiveHash::Associations::ActiveRecordExtensions 3 belongs_to :category 4 belongs_to :condition 5 belongs_to :shipping_fee 6 belongs_to :prefecture 7 belongs_to :send_day 8 belongs_to :user, optional: true 9 has_one :buy 10 has_one_attached :image 11 12 with_options presence: true do 13 validates :image 14 validates :name 15 validates :info 16 validates :price, inclusion: { in: 300..9_999_999 }, format: { with: /\A[0-9]+\z/ } 17 end 18 with_options numericality: { other_than: 1 } do 19 validates :category_id 20 validates :condition_id 21 validates :shipping_fee_id 22 validates :prefecture_id 23 validates :send_day_id 24 end 25 validates :price, numericality: true 26end

投稿2021/01/31 01:33

gomappi

総合スコア7

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

validationはsave直前に行われます。
priceの値はPARAMSで渡ってきた値をpriceの型 integer に合わせて to_i した値が入っているので、save直前には "300dollars" だと 300が入っていて、OKとなります。

型が変換される前の値にバリデーションをかけるを参考にしてください。

投稿2021/01/30 21:58

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gomappi

2021/01/31 00:16

回答ありがとうございます。 itemモデルのpriceをprice_before_type_castに修正したら、正しい値も保存できなくなってしまいました。 何かその後にinteger型に戻す記述等をした方がいいのでしょうか。一応エラー文も掲載しますので確認お願いいたします。 https://gyazo.com/2a969e8fa2047014602838fa2f191645
gomappi

2021/01/31 01:30

すみません。問題が解決出来ましたのでこの質問を解決済みとさせていただきます。 お手数おかけして申し訳ありません。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問