开发者

why my regex is not working in rails 3 app

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.'

I try to use the code above to validate user`s input photo url..

then i randomly get a list of photo url from internet to tests it. but why it complaign the following link is not valid??

http://ps2media.ign.com/ps2/image/article/989/989881/marvel-super-hero-squad-20090602043335342.jpg

Below is the full source code from my User model

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

controller part:

  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

Log message:

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"} 


the regex is valid, probably other validations don't match. you can find the error by using of the following code

@article.valid?
logger.error @article.errors.full_messages


You regex seems to check out. I used Rubular Just a thought: Is your pic_url attr_accessible from where ever you are using it from?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜