4

编辑:一个问题是我需要做 r = Resource.where(id: 3).first #<== 我之前没有 .first

我正在尝试使用 imgkit + paperclip + s3 获取 URL 的快照。我能够获取快照,并将它们成功上传到回形针。例如,本文底部的 IRB 会话成功地将指定 URL 的快照上传到我的 S3 存储桶。

但是,我正在尝试显示图像,但我总是得到一个 missing.png 图像。我在下面给出的 IRB 会话中进行了实验,发现了一些有趣的东西。我认为我没有正确保存 resource.snapshot,因为如果我在不同的变量名下找到资源(查看下面的 IRB 会话),我将无法再次获取该 url。

我只想知道如何获取上传到 S3 的图像的 url。

我运行了“rails 生成回形针资源快照”,然后成功运行了“rake db:migrate”。

宝石文件:

# IMGKit for site thumbnails
gem "imgkit", "~> 1.3.7"

# Amazon S3 storage
gem 'aws-sdk', '~> 1.3.4'

# Paperclip for file uploads
gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"

在我的 resource.rb 模型中:

attr_accessible :snapshot
  has_attached_file :snapshot,
                    :storage => :s3,
                    :s3_credentials => "#{Rails.root}/config/s3.yml"

这是我的 IRB 会议:

128-110-88-73:KnowledgeThief dainewinters$ rails c
    Loading development environment (Rails 3.2.11)
    irb(main):001:0> r = Resource.first
      Resource Load (0.9ms)  SELECT "resources".* FROM "resources" LIMIT 1
    => nil
    irb(main):002:0> r = Resource.first
      Resource Load (1.1ms)  SELECT "resources".* FROM "resources" LIMIT 1
    => #<Resource id: 1, link: "http://cm.dce.harvard.edu/2012/01/13836/publication...", title: "Harvard CS61 - Computer Systems", description: "Harvard's version of CS4400. Topics include assembl...", user_id: nil, created_at: "2013-02-03 20:36:01", updated_at: "2013-02-03 20:36:01", youtubeID: nil, media_type: "video", snapshot_file_name: nil, snapshot_content_type: nil, snapshot_file_size: nil, snapshot_updated_at: nil>
    irb(main):003:0> url = r.link
    => "http://cm.dce.harvard.edu/2012/01/13836/publicationListing.shtml"
    irb(main):004:0> url = PostRank::URI.clean(r.link)
    => "http://cm.dce.harvard.edu/2012/01/13836/publicationListing.shtml"
    irb(main):005:0> side_size = 300
    => 300
    irb(main):006:0> crop_side_size = 300
    => 300
    irb(main):007:0> kit = IMGKit.new(url, :quality => 50,
    irb(main):008:1*                           :width   => side_size,
    irb(main):009:1*                           :height  => side_size,
    irb(main):010:1*                           "crop-w" => crop_side_size,
    irb(main):011:1*                           "crop-h" => crop_side_size,
    irb(main):012:1*                           "zoom"   => 0.35,
    irb(main):013:1*                           "disable-smart-width" => true,
    irb(main):014:1*                           "load-error-handling" => "ignore")
    => #<IMGKit:0x007fa11599adc8 @options={:quality=>50, :width=>300, :height=>300, "crop-w"=>300, "crop-h"=>300, "zoom"=>0.35, "disable-smart-width"=>true, "load-error-handling"=>"ignore"}, @stylesheets=[], @source=http://cm.dce.harvard.edu/2012/01/13836/publicationListing.shtml>
    irb(main):015:0> img = kit.to_img(:jpg)
    => "\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x01..."
    irb(main):016:0> r.id
    => 1
    irb(main):017:0> r.object_id
    => 70164904582860
    irb(main):018:0> file = Tempfile.new(["resource_snapshot_#{r.id}
    irb(main):019:2" ", 'jpg'], 'tmp', :encoding => 'ascii-8bit')
    => #<File:/Users/dainewinters/rails/KnowledgeThief/tmp/resource_snapshot_1
    20130203-28526-1fpqdwljpg>
    irb(main):020:0> file.write(img)
    => 9756
    irb(main):021:0> file.flush
    => #<File:/Users/dainewinters/rails/KnowledgeThief/tmp/resource_snapshot_1
    20130203-28526-1fpqdwljpg>
    irb(main):022:0> r.snapshot
    => /snapshots/original/missing.png
    irb(main):023:0> r.snapshot = file
    => #<File:/Users/dainewinters/rails/KnowledgeThief/tmp/resource_snapshot_1
    20130203-28526-1fpqdwljpg>
    irb(main):024:0> r.snapshot
    => http://s3.amazonaws.com/knowledgethief-dev/resources/snapshots/000/000/001/original/resource_snapshot_1%0A20130203-28526-1fpqdwljpg?1359924148
    irb(main):025:0> r.save!
       (0.2ms)  BEGIN
       (0.9ms)  UPDATE "resources" SET "snapshot_file_name" = 'resource_snapshot_1
    20130203-28526-1fpqdwljpg', "snapshot_content_type" = 'image/jpeg', "snapshot_file_size" = 9756, "snapshot_updated_at" = '2013-02-03 20:42:28.408205', "updated_at" = '2013-02-03 20:42:35.031108' WHERE "resources"."id" = 1
       (0.8ms)  COMMIT
    => true
    irb(main):026:0> r
    => #<Resource id: 1, link: "http://cm.dce.harvard.edu/2012/01/13836/publication...", title: "Harvard CS61 - Computer Systems", description: "Harvard's version of CS4400. Topics include assembl...", user_id: nil, created_at: "2013-02-03 20:36:01", updated_at: "2013-02-03 20:42:35", youtubeID: nil, media_type: "video", snapshot_file_name: "resource_snapshot_1\n20130203-28526-1fpqdwljpg", snapshot_content_type: "image/jpeg", snapshot_file_size: 9756, snapshot_updated_at: "2013-02-03 20:42:28">
    irb(main):027:0> r.snapshot
    => http://s3.amazonaws.com/knowledgethief-dev/resources/snapshots/000/000/001/original/resource_snapshot_1%0A20130203-28526-1fpqdwljpg?1359924148
    irb(main):028:0> file.unlink
    => nil
    irb(main):029:0> r.snapshot
    => http://s3.amazonaws.com/knowledgethief-dev/resources/snapshots/000/000/001/original/resource_snapshot_1%0A20130203-28526-1fpqdwljpg?1359924148
    irb(main):030:0> r.snapshot.url
    => "http://s3.amazonaws.com/knowledgethief-dev/resources/snapshots/000/000/001/original/resource_snapshot_1%0A20130203-28526-1fpqdwljpg?1359924148"
    irb(main):031:0> r2 = Resource.first
      Resource Load (2.3ms)  SELECT "resources".* FROM "resources" LIMIT 1
    => #<Resource id: 2, link: "http://www.codeschool.com/courses/try-git", title: "Try Git", description: "A great introduction to Git, a popular version cont...", user_id: nil, created_at: "2013-02-03 20:36:01", updated_at: "2013-02-03 20:36:01", youtubeID: nil, media_type: "interactive course", snapshot_file_name: nil, snapshot_content_type: nil, snapshot_file_size: nil, snapshot_updated_at: nil>
    irb(main):032:0> r2
    => #<Resource id: 2, link: "http://www.codeschool.com/courses/try-git", title: "Try Git", description: "A great introduction to Git, a popular version cont...", user_id: nil, created_at: "2013-02-03 20:36:01", updated_at: "2013-02-03 20:36:01", youtubeID: nil, media_type: "interactive course", snapshot_file_name: nil, snapshot_content_type: nil, snapshot_file_size: nil, snapshot_updated_at: nil>
    irb(main):033:0> r2.snapshot
    => /snapshots/original/missing.png
    irb(main):034:0> r
    => #<Resource id: 1, link: "http://cm.dce.harvard.edu/2012/01/13836/publication...", title: "Harvard CS61 - Computer Systems", description: "Harvard's version of CS4400. Topics include assembl...", user_id: nil, created_at: "2013-02-03 20:36:01", updated_at: "2013-02-03 20:42:35", youtubeID: nil, media_type: "video", snapshot_file_name: "resource_snapshot_1\n20130203-28526-1fpqdwljpg", snapshot_content_type: "image/jpeg", snapshot_file_size: 9756, snapshot_updated_at: "2013-02-03 20:42:28">
    irb(main):035:0> r2 = Resource.where(id: 1)
      Resource Load (0.6ms)  SELECT "resources".* FROM "resources" WHERE "resources"."id" = 1
    => [#<Resource id: 1, link: "http://cm.dce.harvard.edu/2012/01/13836/publication...", title: "Harvard CS61 - Computer Systems", description: "Harvard's version of CS4400. Topics include assembl...", user_id: nil, created_at: "2013-02-03 20:36:01", updated_at: "2013-02-03 20:42:35", youtubeID: nil, media_type: "video", snapshot_file_name: "resource_snapshot_1\n20130203-28526-1fpqdwljpg", snapshot_content_type: "image/jpeg", snapshot_file_size: 9756, snapshot_updated_at: "2013-02-03 20:42:28">]
    irb(main):036:0> r2
    => [#<Resource id: 1, link: "http://cm.dce.harvard.edu/2012/01/13836/publication...", title: "Harvard CS61 - Computer Systems", description: "Harvard's version of CS4400. Topics include assembl...", user_id: nil, created_at: "2013-02-03 20:36:01", updated_at: "2013-02-03 20:42:35", youtubeID: nil, media_type: "video", snapshot_file_name: "resource_snapshot_1\n20130203-28526-1fpqdwljpg", snapshot_content_type: "image/jpeg", snapshot_file_size: 9756, snapshot_updated_at: "2013-02-03 20:42:28">]
    irb(main):037:0> r2.snapshot
    NoMethodError: undefined method `snapshot' for #<ActiveRecord::Relation:0x007fa115d42b88>
        from /Users/dainewinters/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/delegation.rb:45:in `method_missing'
        from (irb):37
        from /Users/dainewinters/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
        from /Users/dainewinters/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
        from /Users/dainewinters/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:12:in `require'
        from script/rails:12:in `<main>'
    irb(main):038:0> r2.snapshot_file_name
    NoMethodError: undefined method `snapshot_file_name' for #<ActiveRecord::Relation:0x007fa115d42b88>
        from /Users/dainewinters/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/relation/delegation.rb:45:in `method_missing'
        from (irb):38
        from /Users/dainewinters/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
        from /Users/dainewinters/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
        from /Users/dainewinters/.rbenv/versions/1.9.3-p327-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:12:in `require'
        from script/rails:12:in `<main>'
    irb(main):039:0> r2
    => [#<Resource id: 1, link: "http://cm.dce.harvard.edu/2012/01/13836/publication...", title: "Harvard CS61 - Computer Systems", description: "Harvard's version of CS4400. Topics include assembl...", user_id: nil, created_at: "2013-02-03 20:36:01", updated_at: "2013-02-03 20:42:35", youtubeID: nil, media_type: "video", snapshot_file_name: "resource_snapshot_1\n20130203-28526-1fpqdwljpg", snapshot_content_type: "image/jpeg", snapshot_file_size: 9756, snapshot_updated_at: "2013-02-03 20:42:28">]
    irb(main):040:0> 
4

1 回答 1

0

事实证明,我做的一切都是对的,除了:

r = Resource.where(id: 3)

需要是

r = Resource.where(id: 3).first

因为第一个返回一个数组。

于 2013-02-03T22:03:43.917 回答