Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 如何在ActiveModel::EachValidator内创建数组_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 如何在ActiveModel::EachValidator内创建数组

Ruby on rails 4 如何在ActiveModel::EachValidator内创建数组,ruby-on-rails-4,Ruby On Rails 4,我是RubyonRails4的新手。我已经创建了一个自定义验证器,但无法为数组赋值。它显示了错误 未定义的方法如果构造函数名称中有输入错误,则应该是initialize而不是initilize。这就是@msg变量未定义的原因-从未调用过构造函数 祝你今天愉快。谢谢。我太糊涂了!:另外,不确定如何在初始化中获取密码。我已经更新了我的帖子,请检查@val行。知道吗?是的,我可以在每个记录、属性、值中找到它。但是,我想知道我可以在初始化时得到它吗?我想,没有。为什么需要它呢?只是想分配初始化下的所有值

我是RubyonRails4的新手。我已经创建了一个自定义验证器,但无法为数组赋值。它显示了错误
未定义的方法如果构造函数名称中有输入错误,则应该是initialize而不是initilize。这就是@msg变量未定义的原因-从未调用过构造函数


祝你今天愉快。

谢谢。我太糊涂了!:另外,不确定如何在初始化中获取密码。我已经更新了我的帖子,请检查@val行。知道吗?是的,我可以在每个记录、属性、值中找到它。但是,我想知道我可以在初始化时得到它吗?我想,没有。为什么需要它呢?只是想分配初始化下的所有值。如果可能的话!
class User < ActiveRecord::Base

    has_secure_password
    validates :email,:email_format => true, :on => :create
    validates :password, password_format:{with: "upercase"}
end
class PasswordFormatValidator < ActiveModel::EachValidator

  #def initilize(options)-Fixed
  def initialize(options)
  @msg=[]
  @password1 = options[:attributes=>[:password]]
  @val=options.inspect
  super
  end

  def validate_each(record, attribute, value)
   record.errors[attribute] << @val

    unless (value.nil? || value.empty?)
    @msg << 1
    #record.errors[attribute] << "testing"

    end

 end

end
{:attributes=>[:password], :complexity=>3, :length=>6, :class=>User(id: integer, email: string, password_digest: string, created_at: datetime, updated_at: datetime)}