Rails 4 个带验证的无表模型
Rails 4 Tableless Model with Validations
在rails 4.0.5我建立了一个tableless模型。所以我有一个获取信息的表格,但是当我尝试验证它确实有效的信息时。验证将 return 'canot be blank' 对所有字段进行验证,即使该字段已填充。我做错了什么。
形式
= form_for @bank do |f|
= f.text_field :first_name
= f.text_field :account
= f.text_field :routing
= f.text_field :zip
%button{:type => "submit"} Submit
控制器
class BanksController < ApplicationController
def new
@bank = Bank.new
end
def create
@bank = Bank.new(params[bank_params])
if @bank.valid?
# etc....
end
end
型号
class Bank < ActiveRecord::Base
def self.columns() @columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
column :first_name, :string
column :account, :int
column :routing, :int
column :zip, :int
VALID_NUM_REGEX = /\A[+-]?\d+\z/
validates :first_name, presence: true
validates :account, presence: true, format: { with: VALID_NUM_REGEX }, length: { minimum: 4 }
validates :routing, presence: true, format: { with: VALID_NUM_REGEX }, length: { minimum: 4 }
end
控制台
Started POST "/banks" for 127.0.0.1 at 2015-03-09 11:39:40 -0400
Processing by BanksController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"..=","bank"=>{"first_name"=>"Alain", "account"=>"1234", "routing"=>"4321","zip"=>"11413"}}
我不知道出了什么问题,但我猜这与您覆盖 columns
和 column
有关。我强烈建议您使用普通的 Ruby 对象,然后包含 ActiveModel::Validations.
http://api.rubyonrails.org/classes/ActiveModel/Validations.html
来自该页面:
class Person
include ActiveModel::Validations
attr_accessor :first_name, :last_name
validates_each :first_name, :last_name do |record, attr, value|
record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
end
end
它为您提供了您从 Active Record 了解到的完整标准验证堆栈:
person = Person.new
person.valid? # => true
person.invalid? # => false
person.first_name = 'zoolander'
person.valid? # => false
person.invalid? # => true
person.errors.messages # => {first_name:["starts with z."]}
在rails 4.0.5我建立了一个tableless模型。所以我有一个获取信息的表格,但是当我尝试验证它确实有效的信息时。验证将 return 'canot be blank' 对所有字段进行验证,即使该字段已填充。我做错了什么。
形式
= form_for @bank do |f|
= f.text_field :first_name
= f.text_field :account
= f.text_field :routing
= f.text_field :zip
%button{:type => "submit"} Submit
控制器
class BanksController < ApplicationController
def new
@bank = Bank.new
end
def create
@bank = Bank.new(params[bank_params])
if @bank.valid?
# etc....
end
end
型号
class Bank < ActiveRecord::Base
def self.columns() @columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
column :first_name, :string
column :account, :int
column :routing, :int
column :zip, :int
VALID_NUM_REGEX = /\A[+-]?\d+\z/
validates :first_name, presence: true
validates :account, presence: true, format: { with: VALID_NUM_REGEX }, length: { minimum: 4 }
validates :routing, presence: true, format: { with: VALID_NUM_REGEX }, length: { minimum: 4 }
end
控制台
Started POST "/banks" for 127.0.0.1 at 2015-03-09 11:39:40 -0400
Processing by BanksController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"..=","bank"=>{"first_name"=>"Alain", "account"=>"1234", "routing"=>"4321","zip"=>"11413"}}
我不知道出了什么问题,但我猜这与您覆盖 columns
和 column
有关。我强烈建议您使用普通的 Ruby 对象,然后包含 ActiveModel::Validations.
http://api.rubyonrails.org/classes/ActiveModel/Validations.html
来自该页面:
class Person
include ActiveModel::Validations
attr_accessor :first_name, :last_name
validates_each :first_name, :last_name do |record, attr, value|
record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
end
end
它为您提供了您从 Active Record 了解到的完整标准验证堆栈:
person = Person.new
person.valid? # => true
person.invalid? # => false
person.first_name = 'zoolander'
person.valid? # => false
person.invalid? # => true
person.errors.messages # => {first_name:["starts with z."]}