EDIT: One problem was I need to do r = Resource.where(id: 3).first #<== I didn't have the .first before
I'm attempting to get snapshots of URLs by using imgkit + paperclip + s3. I am able to get the snapshots, and successfully upload them to paperclip. For example, the IRB session at the bottom of this post successfully uploads a snapshot of the specified URL to my S3 bucket.
However, I am trying to display the images, and I always get a missing.png image. I experimented in the IRB session given below and found something interesting. I don't think that I am saving resource.snapshot correctly, because I can't get that url again if I find the resource under a different variable name (look at the IRB session below).
All I want to know is how I can get the url of the image I uploaded to S3.
I ran "rails generate paperclip resource snapshot" and then "rake db:migrate" successfully.
Gemfile:
# 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"
In my resource.rb model:
attr_accessible :snapshot
has_attached_file :snapshot,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml"
Here's my IRB session:
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>
Turns out, I was doing everything right except:
r = Resource.where(id: 3)
Needs to be
r = Resource.where(id: 3).first
As the first one returns an array.