2011-12-23 60 views
2

背景:我正在用Padrino,Ruby 1.9.2, HAML和Heroku構建一個簡單的博客。我希望帖子索引擁有類似雜誌的外觀 。對於索引中的每個帖子,用戶都應該看到標題,內容 挑逗者和圖片。我的問題是附加圖像。與Padrino一起使用回形針

問題:我一直在嘗試使用Paperclip gem將圖像 附加到每個帖子。 Heroku將從亞馬遜S3倉中捕捉實際圖像。 到目前爲止,當我嘗試運行遷移時,我得到的錯誤:

​​

它看起來像回形針就不會加載,但有從哪裏獲得 卡住。

詳情: - 問:當我「嘗試運行遷移」時,我正在做什麼? --a:我把下列的遷移文件夾

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 

然後運行 ​​「padrino耙AR:遷移」

--Q:我有回形針在Gemfile中列出? - 答:是的。

- 問:郵政模型是什麼樣的? --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:什麼是app.rb樣子? --A:

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

    enable :sessions 

    get '/' do 
    haml :index 
    end 
end 

- 問:boot.rb是什麼樣子的? --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! 

- 問:完整的錯誤輸出是什麼樣的? --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>' 

如果你已經讀到這裏...謝謝!我希望你能幫助我解決這個問題。

+0

沒有遷移問題!我修正了遷移,應該讀取「t.string」而不是「t.has_file_attached」:P但是,當我嘗試運行應用程序時,仍然收到相同的錯誤 – haiqus 2011-12-23 05:16:38

+0

imagemagick已安裝?你在使用rvm和/或bundler嗎? – froderik 2011-12-23 07:41:38

+0

imagemagick安裝最後我檢查。我會仔細檢查確認。我使用捆綁器,沒有任何困難 – haiqus 2011-12-31 01:54:38

回答

2

回形針是rails不可知的,所以你需要把boot.rbPadrino.before_load這樣的:

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

    Paperclip.options[:logger] = Padrino.logger 

heroku下最後運行遷移,你應該先運行本地padrino rake gen這將創建一個Rakefile所以你應該能夠簡單地運行:heroku rake ar:migrate

+0

謝謝!我在這個項目上休息了一下......我很快就會讓你知道你的建議是如何運作的 – haiqus 2011-12-31 01:55:51

+0

好吧......我得到了「未初始化的常量Paperclip :: Schema(NameError)」以及建議的添加boot.rb – haiqus 2011-12-31 02:37:53

+0

是的,對不起,我使用了主分支,現在我已經更新了代碼以使用2.4.5版本的回形針。 – DAddYE 2012-01-02 10:49:45