Using Paperclip with Padrino

2019-05-26 22:50发布

问题:

Background: I'm building a simple blog using Padrino, Ruby 1.9.2, HAML, and Heroku. I want the posts index to have a magazine-like look to it. For each post in the index, the user should see titles, content teasers, and images. My problem is with attaching the images.

Problem: I've been trying to use the Paperclip gem to attach an image to each post. Heroku will fish the actual image from an Amazon S3 bin. So far, when I try to run a migration, I get the error:

$ .../base.rb:1088:in `method_missing': undefined method
`has_attached_file' for #<Class:0xa672140> (NoMethodError)

It looks like Paperclip just won't load, but there's where I get stuck.

Details: --Q: What am I doing when I "try to run a migration"? --A: I put the following into the migrations folder

class AddPictureToPost < ActiveRecord::Migration
  def self.up
    change_table :posts do |t|
      t.has_attached_file :picture
    end
  end

  def self.down
    drop_attached_file :posts, :picture
  end
end

then run "padrino rake ar:migrate"

--Q: Do I have Paperclip listed in the Gemfile? --A: Yes.

--Q: What does the post model look like? --A:

class Post < ActiveRecord::Base
  has_attached_file :picture,
    :storage => :s3,
    :bucket => 'blog_images',
    :s3_credentials => {
      :access_key_id => ENV['S3_KEY'],
      :secret_access_key => ENV['S3_SECRET']
    }
end

--Q: What does the app.rb look like? --A:

class Blog < Padrino::Application
  register Padrino::Rendering
  register Padrino::Mailer
  register Padrino::Helpers

  enable :sessions

  get '/' do
    haml :index
  end
end

--Q: What does boot.rb look like? --A:

PADRINO_ENV  = ENV["PADRINO_ENV"] ||= ENV["RACK_ENV"] ||=
"development"  unless defined?(PADRINO_ENV)
PADRINO_ROOT = File.expand_path('../..', __FILE__) unless defined?
(PADRINO_ROOT)

require 'rubygems' unless defined?(Gem)
require 'bundler/setup'
Bundler.require(:default, PADRINO_ENV)

Padrino.before_load do
end

Padrino.after_load do
end

Padrino.load!

--Q: What does the full error output look like? --A:

jared:~/blog$ padrino rake ar:migrate
=> Executing Rake ar:migrate ...
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/
active_record/base.rb:1088:in `method_missing': undefined method
`has_attached_file' for #<Class:0xa1ffb30> (NoMethodError)
        from /home/jared/isthmus2/app/models/post.rb:2:in `<class:Post>'
        from /home/jared/isthmus2/app/models/post.rb:1:in `<top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:240:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:240:in `block in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:223:in `block in load_dependency'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:640:in `new_constants_in'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:223:in `load_dependency'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:240:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/reloader.rb:148:in `safe_load'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:162:in `block in require_dependencies'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:160:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:160:in `require_dependencies'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/application.rb:34:in `inherited'
        from /home/jared/blog/app/app.rb:1:in `<top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:240:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:240:in `block in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:223:in `block in load_dependency'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:640:in `new_constants_in'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:223:in `load_dependency'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:240:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/reloader.rb:148:in `safe_load'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:162:in `block in require_dependencies'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:160:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:160:in `require_dependencies'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/mounter.rb:148:in `locate_app_object'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/mounter.rb:30:in `initialize'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/mounter.rb:221:in `new'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/mounter.rb:221:in `mount'
        from /home/jared/blog/config/apps.rb:34:in `<top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:240:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:240:in `block in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:223:in `block in load_dependency'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:640:in `new_constants_in'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:223:in `load_dependency'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/
active_support/dependencies.rb:240:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/reloader.rb:148:in `safe_load'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:162:in `block in require_dependencies'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:160:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:160:in `require_dependencies'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:70:in `block in load!'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:70:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/loader.rb:70:in `load!'
        from /home/jared/blog/config/boot.rb:29:in `<top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/cli/base.rb:56:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/cli/base.rb:56:in `block in rake'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/cli/base.rb:140:in `capture'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/lib/
padrino-core/cli/base.rb:56:in `rake'
        from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/task.rb:
22:in `run'
        from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/
invocation.rb:118:in `invoke_task'
        from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor.rb:
263:in `dispatch'
        from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/base.rb:
389:in `start'
        from /usr/local/lib/ruby/gems/1.9.1/gems/padrino-core-0.10.5/bin/
padrino:9:in `<top (required)>'
        from /usr/local/bin/padrino:19:in `load'
        from /usr/local/bin/padrino:19:in `<main>'

If you've read this far...THANKS! I hope you can help me shed some light on this.

回答1:

Paperclip is rails agnostic so you need to put in boot.rb in Padrino.before_load this:

  ActiveRecord::Base.send(:include, Paperclip::Glue)
  File.send(:include, Paperclip::Upfile)

  Paperclip.options[:logger] = Padrino.logger

Finally to run migrations under heroku you should run first locally padrino rake gen this will create a Rakefile so you should able to run simply: heroku rake ar:migrate