Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 回形针不';t以正确的格式存储通过url获取的图像_Ruby On Rails_Paperclip_Omniauth_Omniauth Facebook - Fatal编程技术网

Ruby on rails 回形针不';t以正确的格式存储通过url获取的图像

Ruby on rails 回形针不';t以正确的格式存储通过url获取的图像,ruby-on-rails,paperclip,omniauth,omniauth-facebook,Ruby On Rails,Paperclip,Omniauth,Omniauth Facebook,当我试图通过graph URL()获取facebook图像时,曲别针会将头像图像名称作为“图片”存储在数据库中。代码示例: img = UserImages.new img.avatar = URI.parse('https://graph.facebook.com/666980153384194/picture?type=large') img.save 控制台日志中的MySQL查询: => #<URI::HTTPS:0x00000009954490 URL:https://g

当我试图通过graph URL()获取facebook图像时,曲别针会将头像图像名称作为“图片”存储在数据库中。代码示例:

img =  UserImages.new
img.avatar = URI.parse('https://graph.facebook.com/666980153384194/picture?type=large')
img.save
控制台日志中的MySQL查询:

=> #<URI::HTTPS:0x00000009954490 URL:https://graph.facebook.com/666980153384194/picture?type=large>
irb(main):009:0> img.save
   (0.0ms)  BEGIN
  SQL (0.0ms)  INSERT INTO `user_images` (`avatar_content_type`, `avatar_file_name`, `avatar_file_size`, `avatar_updated
_at`, `created_at`, `updated_at`) VALUES ('image/jpeg', 'picture', 6157, '2014-07-08 13:55:53', '2014-07-08 13:56:02', '
2014-07-08 13:56:02')
   (29.1ms)  COMMIT
获取的图像存储为:

   (0.0ms)  BEGIN
Command :: file -b --mime "C:/Users/Windows/AppData/Local/Temp/f1620d075c0642a77f7b98e532d8a8eb20140708-1040-1h5z4e6.jpg
"
  SQL (1.0ms)  INSERT INTO `user_images` (`avatar_content_type`, `avatar_file_name`, `avatar_file_size`, `avatar_updated
_at`, `created_at`, `updated_at`) VALUES ('image/jpeg', '10478546_662150043867205_2640371404472615909_n.jpg', 25862, '20
14-07-08 14:02:45', '2014-07-08 14:02:49', '2014-07-08 14:02:49')
   (34.1ms)  COMMIT
=> true
如有任何建议(解决方案),将不胜感激

UserImages类的源:

  class UserImages < ActiveRecord::Base
    belongs_to :imageable ,polymorphic: true
    has_attached_file :avatar,
                      :styles => { :medium => "300x300>", :thumb => "125x125>" },
                      :path => ":rails_root/public/images/users/:id/:style/:hash.:extension",
                      :default_url => "/images/normal/missing.jpg",
                      :url => "/images/users/:id/:style/:hash.:extension",
                      :hash_secret => "EWRWerrew234UTY"

    validates_attachment :avatar, :content_type =>  { :content_type => ["image/jpeg", "image/gif", "image/png"] },
                                      :size => { :in => 0..5.megabytes }

  end
classuserimages{:medium=>“300x300>”,:thumb=>“125x125>”,
:path=>“:rails\u root/public/images/users/:id/:style/:hash.:extension”,
:default_url=>“/images/normal/missing.jpg”,
:url=>“/images/users/:id/:style/:hash.:extension”,
:hash_secret=>“EWRWerrew234UTY”
验证_附件:头像,:content_type=>{:content_type=>[“image/jpeg”、“image/gif”、“image/png”]},
:size=>{:in=>0..5.MB}
结束

两种获取URL都没有问题。。。。两者都很好。。。 区别只是因为回形针从URL的最后一部分生成文件名。。。 所以在第一个URL中

图片?类型=大

在第二个URL中

10478546_662150043867205_2640371404472615909_n.jpg?oh=cb2fe9d421fef3d7d2220bb48a2a36e2&oe=5418E8FB

我已经将回形针用来生成文件名的URL部分加粗了。。。。在这两种情况下,你不应该为文件名而烦恼。。。因为你可以做任何你想做的手术


您可以通过检查路径
public/images/users/:id
(回形针路径配置)

来确保文件已上载。非常感谢您的澄清!:)是,图像按指定路径存储。名称文件具有散列格式,但没有文件扩展名:d8146a48c0f4fc3d916e57257c6270e91205ccbc它对图像的解析方式有点混淆:PYeah如果回形针无法计算出将发生的扩展名。。。如果该文件存在于该路径中,您可以将其打开。然后就可以用回形针了。
  class UserImages < ActiveRecord::Base
    belongs_to :imageable ,polymorphic: true
    has_attached_file :avatar,
                      :styles => { :medium => "300x300>", :thumb => "125x125>" },
                      :path => ":rails_root/public/images/users/:id/:style/:hash.:extension",
                      :default_url => "/images/normal/missing.jpg",
                      :url => "/images/users/:id/:style/:hash.:extension",
                      :hash_secret => "EWRWerrew234UTY"

    validates_attachment :avatar, :content_type =>  { :content_type => ["image/jpeg", "image/gif", "image/png"] },
                                      :size => { :in => 0..5.megabytes }

  end