Regex 为什么我的正则表达式不能在rails 3应用程序中工作

Regex 为什么我的正则表达式不能在rails 3应用程序中工作,regex,ruby-on-rails-3,Regex,Ruby On Rails 3,我尝试使用上面的代码验证用户输入的照片url 然后我从互联网上随机获得一个照片url列表来测试它。 但为什么它抱怨下面的链接无效 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,

我尝试使用上面的代码验证用户输入的照片url

然后我从互联网上随机获得一个照片url列表来测试它。 但为什么它抱怨下面的链接无效

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
日志消息:

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

你的正则表达式似乎要退出了。我只是想:你的图片url
attr\u可以从你使用它的地方访问吗?

你用什么上传照片?例如,如果你使用的是回形针gem,那么它已经内置了你可以使用的验证。。。也许应该改为上传。。。谢谢你的评论和建议。你好,米哈伊洛夫,只有一个错误,那就是:pic_url和我指定的消息。问题是由于组合了allow_blank、allow_nil和。尝试使用条件表达式测试blank/nilHi-mikhailov,而不是下面的内容,如果我删除allow\u blank和allow\u nil,它将始终抱怨是否存在空字符串。设计是,我想允许url为空。好的,你能复制控制器的部分吗?嗨,米哈伊洛夫。。。是的。。。控制器代码也复制到问题中。查看更新。。。
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"} 
@article.valid?
logger.error @article.errors.full_messages