0

文字列であるscreen_nameという行を持つusersテーブルがあります。

他の制約の中でも、画面名に。のような文字を含めることはできません。、&​​%@など。この目的のために、次のバリデーターを作成しました。

validates :screen_name, presence: true,
          length: { maximum: 15 },
          uniqueness: { case_sensitive: false },
          format: { with: /\w+/ }

次にfoo.barのようなスクリーン名を入力すると、それは喜んで受け入れられ、データベースに保存されました。

私は何が間違っているのですか?

# == Schema Information
#
# Table name: users
#
#  id              :integer          not null, primary key
#  name            :string(255)
#  email           :string(255)
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#  password_digest :string(255)
#  remember_token  :string(255)
#  admin           :boolean          default(FALSE)
#  screen_name     :string(255)    
#
4

1 に答える 1

1

このように検証を変更します/^\w*$/

また

このように検証できます

validates_format_of :screen_name, :with => /^[A-Za-z0-9.&]*\z/
于 2013-03-20T13:28:15.090 に答える