validates_format_of :pic_url, :allow_nil => true, :allow_blank => true,
:with => /\A^(http|https|ftp|www):[\/A-Za-z0-9-~.&@\#$%_]*(img|jpg|jpeg|png|bmp|gif)$\Z/i,
:message => 'Invalid photo url.'
我尝试使用上面的代码来验证用户输入的照片网址..
然后我随机从互联网上获取一张照片网址列表进行测试。 但为什么它抱怨以下链接无效?
http://ps2media.ign.com/ps2/image/article/989/989881/marvel-super-hero-squad-20090602043335342.jpg
以下是我的用户模型的完整源代码
class User
include MongoMapper::Document
GENDERS = {1 => 'Male', 2 => 'Female', 3 => 'Secret'}
validates :email, :presence =>true, :uniqueness => true
validates_format_of :email, :with => /\A^[^\r\n@ ][^\r\n@ ]+@[^\r\n@ ]+[.][^\r\n@. ]+$\Z/i,
:message => 'Invalid email format'
validates :password, :confirmation =>true
validate :password_must_be_present
validates :gender, :presence => true
# This is the line that causing problem......
validates_format_of :profile_pic_url, :allow_nil => true,
:allow_blank => true,
:with => /\A^(http|https|ftp|www):[\/A-Za-z0-9-~.&@\#$%_]*(img|jpg|jpeg|png|bmp|gif)$\Z/i,
:message => 'Please use photo from one of the following formats (img|jpg|jpeg|png|bmp|gif).'
key :display_name, String
key :email, String
key :gender, Integer
key :profile_pic_url, String
key :salt, String
key :hashed_password, String
timestamps!
attr_reader :password
def password=(password)
@password = password
if password.present?
generate_salt
self.hashed_password = self.class.encrypt_password(password, salt)
end
end
class << self
def encrypt_password(password, salt)
Digest::SHA2.hexdigest(password + "shrimpy" + salt)
end
def authenticate(email, password)
if user = find_by_email(email)
if user.hashed_password == encrypt_password(password, user.salt)
user
end
end
end
end
private
def password_must_be_present
errors.add(:password, "Missing") unless self.password.present?
errors.add(:password_confirmation, "Missing") unless self.password_confirmation.present?
end
def generate_salt
self.salt = self.object_id.to_s + rand.to_s
end
end
控制器部分:
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(my_account_path, :notice => 'User info was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => '/my_account/index' }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
记录消息:
Started POST "/users/4df196efab90911f50000004" for 127.0.0.1 at 2011-07-19 15:48:10 +1000
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"鉁?, "authenticity_token"=>"wTTv7KW/meevsHyuoVcysGGxCktdLFd3ch2ZlA6pos4=", "user"=>{"email"=>"email@gmail.com", "password"=>"[FILTERED]", "password_c
onfirmation"=>"[FILTERED]", "gender"=>"3", "display_name"=>"Shrimpy", "profile_pic_url"=>"http://ps2media.ign.com/ps2/image/article/989/989881/marvel-super-hero-squad-200906
02043335342.jpg"}, "commit"=>"Update User", "id"=>"4df196efab90911f50000004"} M
ONGODB shrimpy_development['users'].find({:_id=>BSON::ObjectId('4df196efab90911f50000004')}) M
ONGODB shrimpy_development['users'].find({:_id=>BSON::ObjectId('4df196efab90911f50000004')}) =
==> {"email"=>"email@gmail.com", "password"=>"123", "password_confirmation"=>"123", "gender"=>"3", "display_name"=>"Shrimpy", "profile_pic_url"=>"http://ps2media.ign.com/ps
2/image/article/989/989881/marvel-super-hero-squad-20090602043335342.jpg"}
答案 0 :(得分:1)
正则表达式有效,可能其他验证不匹配。您可以使用以下代码找到错误
@article.valid?
logger.error @article.errors.full_messages
答案 1 :(得分:0)
你的正则表达式好像结账了。我使用了Rubular只是一个想法:你的pic_url attr_accessible
来自哪里使用它?