Ruby on rails 为什么我的验证正在运行?

Ruby on rails 为什么我的验证正在运行?,ruby-on-rails,validation,paperclip,Ruby On Rails,Validation,Paperclip,我正在创建具有附件的对象。我通过拆分一个真实字段(可能是我问题的真正根源)来伪造字段。另外,这些都可以正常工作,但是当我附加文件时,我的验证似乎比控制器逻辑运行得更早,并且我最终得到了如下错误: undefined method `match` for nil:Nil at line ... attr_accessible :zipcode has_attached_file :attachment #this is the line referred to by the error mes

我正在创建具有附件的对象。我通过拆分一个真实字段(可能是我问题的真正根源)来伪造字段。另外,这些都可以正常工作,但是当我附加文件时,我的验证似乎比控制器逻辑运行得更早,并且我最终得到了如下错误:

undefined method `match` for nil:Nil at line ...
attr_accessible :zipcode
has_attached_file :attachment

#this is the line referred to by the error message
validates :zipcode, format: { with: /a_meaningful_regex_here/i}

def initialize
  zipcode = '-'
end
def set_zipcode(params={})
  zipcode = "#{params[:zipcode1]}-#{params[:zipcode2]}"
end
def zipcode1
  zipcode.split('-')[0]
end
def zipcode2
  zipcode.split('-')[1]
end
<%= form_for @foo do |f| %>
  <%= f.file_field :attachment %>
  <%= text_field_tag  :zipcode1,@foo.zipcode1%> - 
  <%= text_field_tag  :zipcode2,@foo.zipcode2%>
  <%= f.submit %>
<% end %>
def create
  @foo = Foo.new(params[:foo])
  @foo.set_zipcode(params)
  if @foo.save
    redirect_to @foo
  else
    render action: 'new'
  end
end
验证是否由回形针触发?如何关闭它,或者在验证运行之前运行自己的逻辑

我的模型如下所示:

undefined method `match` for nil:Nil at line ...
attr_accessible :zipcode
has_attached_file :attachment

#this is the line referred to by the error message
validates :zipcode, format: { with: /a_meaningful_regex_here/i}

def initialize
  zipcode = '-'
end
def set_zipcode(params={})
  zipcode = "#{params[:zipcode1]}-#{params[:zipcode2]}"
end
def zipcode1
  zipcode.split('-')[0]
end
def zipcode2
  zipcode.split('-')[1]
end
<%= form_for @foo do |f| %>
  <%= f.file_field :attachment %>
  <%= text_field_tag  :zipcode1,@foo.zipcode1%> - 
  <%= text_field_tag  :zipcode2,@foo.zipcode2%>
  <%= f.submit %>
<% end %>
def create
  @foo = Foo.new(params[:foo])
  @foo.set_zipcode(params)
  if @foo.save
    redirect_to @foo
  else
    render action: 'new'
  end
end
我的表单如下所示:

undefined method `match` for nil:Nil at line ...
attr_accessible :zipcode
has_attached_file :attachment

#this is the line referred to by the error message
validates :zipcode, format: { with: /a_meaningful_regex_here/i}

def initialize
  zipcode = '-'
end
def set_zipcode(params={})
  zipcode = "#{params[:zipcode1]}-#{params[:zipcode2]}"
end
def zipcode1
  zipcode.split('-')[0]
end
def zipcode2
  zipcode.split('-')[1]
end
<%= form_for @foo do |f| %>
  <%= f.file_field :attachment %>
  <%= text_field_tag  :zipcode1,@foo.zipcode1%> - 
  <%= text_field_tag  :zipcode2,@foo.zipcode2%>
  <%= f.submit %>
<% end %>
def create
  @foo = Foo.new(params[:foo])
  @foo.set_zipcode(params)
  if @foo.save
    redirect_to @foo
  else
    render action: 'new'
  end
end
尝试以下方法:

before_validation(:on => :zipcode) { set_zipcode }

您是否尝试在控制器中记录某些内容,只是为了确保在验证之前不会像应该的那样对其进行处理?你确定正则表达式写得像它应该写的那样吗?如果不是,请尝试
/\Aa\u有意义的\u regex\u here\z/i
,因为问题似乎来自这里,如果问题来自zipcode的内容,则不会有nil对象,因为您使用“-”对其进行初始化。。。因此,我认为更可能是使用正则表达式或其语法。