我使用以下正则表达式验证我的rails应用程序中的nic号码:
VALID_NIC_REGEX = /\A\d{9}[V|v|x|X]\z/i
validates :nic, presence: true, allow_blank: true, format: { with: VALID_NIC_REGEX }, uniqueness: { case_sensitive: false }
当我在本地机器上测试时,一切正常。但是在上传到heroku之后,在尝试使用nic字段中的值保存成员时会出错。有效值和无效值都会给出错误。如果nic字段中没有值,则会保存该成员。在heroku日志中,我发现了这个错误:
NoMethodError (undefined method `limit' for nil:NilClass):
app/controllers/members_controller.rb:117:in `update'
所以,我注释掉了两条验证线并将网站上传到了Heroku。现在它有效。有人可以解释一下吗?谢谢。
控制器更新操作:
def update
@source = params[:source]
@telfie = params[:telfie].to_i
@member = Member.find(params[:id])
@tel = Array.new
(1..@telfie.to_i).each do |i|
@tel << params[:tele][:"#{'tele'}#{i}"]
end
#params[:tele] = @tel
if params[:commit] == 'More Numbers'
@telfie = @telfie + 1
render 'edit'
else
if params[:commit] == 'Less Numbers'
if @telfie > 1
@telfie = @telfie - 1
render 'edit'
else
flash.now[:error] = "Can't remove all contact fields!"
render 'edit'
end
else
if @member.update_attributes(member_params)
@member.tele = @tel
@member.save
flash[:success] = "Member #{@member.name} Successfully Updated!"
if params[:commit] == 'Save Member and Add Associates'
redirect_to new_associate_url(memberid: @member.id, source: "memberv" )
else
if @source == "database"
redirect_to database_path
else
redirect_to member_url(@member)
end
end
else
render 'edit'
end
end
end
end