使用回形针与Padrino回形针、Padrino

2023-09-11 09:31:22 作者:Q糖Q

背景:我建立使用Padrino,红宝石1.9.2一个简单的博客, HAML和Heroku上。我想要的职位指数有一本杂志般的外观 给它。对于索引中的各岗位,用户应该看到标题,内容 玩笑和图像。我的问题是其附带的图像。

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.

问题:我一直在试图使用回形针宝石附加图像 到每个岗位。 Heroku的将鱼从Amazon S3的bin中的实际图像。 到目前为止,当我尝试运行迁移时,我得到的错误:

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.

详细信息: --Q:我在做什么,当我尝试运行迁移? --a:我把下列的迁移文件夹

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

然后运行padrino耙AR:迁移

then run "padrino rake ar:migrate"

- Q:我已经回形针在Gemfile中列出? --a:是的,

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

- 问:什么是岗位模型是什么样子? --a:

--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

- 问:什么是app.rb样子? --a:

--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

- 问:你的boot.rb样子呢? --a:

--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!

- 问:什么是完全错误输出是什么样子? --a:

--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.

推荐答案

回形针是轨道不可知的,所以你需要把的boot.rb Padrino.before_load 这样的:

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

最后跑在的Heroku 迁移,你应该先运行本地 padrino耙创这将创建一个 Rake文件所以你应该能够简单地运行: Heroku的耙AR:迁移

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