我在我的一个模型中进行了验证:( sku_code验证。)
class Vendor < ActiveRecord::Base
has_many :purchases
validates :name, format: {with: /\A[A-Za-z0-9\S\s]+\z/, message: "Vendor name is invalid."}
validates :sku_code, format: {with: /\A[A-Z]{3}\z/, message: "must follow format ABC"}
end
控制器:
class VendorsController < ApplicationController
def index
@vendors = Vendor.all
respond_to do |format|
format.js
format.html
format.json
end
end
def new
@vendor = Vendor.new
respond_to do |format|
format.js
format.html
format.json
end
end
def create
@vendor = Vendor.new(vendor_params)
respond_to do |format|
if @vendor.save
format.html { redirect_to @vendor, notice: 'Vendor successfully created.' }
format.js {}
format.json { render json: @vendor, status: :created, location: @vendor }
else
format.html { render 'new' }
format.json { render json: @vendor.errors, status: :unprocessable_entity }
end
end
end
def update
@vendor = Vendor.find(params[:id])
respond_to do |format|
if @vendor.update(vendor_params)
format.html { redirect_to @vendor, notice: 'Vendor successfully updated.' }
format.js {}
format.json { render json: @vendor, status: :updated, location: @vendor }
else
format.html { render 'edit' }
format.json { render json: @vendor.errors, status: :unprocessable_entity }
end
end
end
def edit
@vendor = Vendor.find(params[:id])
respond_to do |format|
format.js
format.html
format.json
end
end
def destroy
@vendor = Vendor.find(params[:id])
@vendor.destroy
#flash.notice="Vendor '#{@vendor.name}' was deleted."
redirect_to action: "index"
end
def show
@vendor = Vendor.find(params[:id])
respond_to do |format|
format.js
format.html
format.json
end
end
private
def vendor_params
params.require(:vendor).permit(:name, :sku_code, :contact, :phone, :email, :address_line1, :address_line2, :address_city, :address_state, :address_zip, :address_country, :lead_time)
end
end
我对这个正则表达式的期望是它允许将三个大写字母字符串输入到我的表单中但是阻止其他任何内容。
目前它正在拒绝三个大写字母字符串。
我在这里做错了什么?