【问题标题】:Paperclip with S3 - undefined method带有 S3 的回形针 - 未定义的方法
【发布时间】:2012-06-07 22:18:56
【问题描述】:

我正在尝试在我的 Rails 3.2 应用程序中将 Paperclip gem 与 A​​mazon S3 一起使用。但是,当我添加 :storage => :s3 和 S3 信息并生成表单时出现问题。

错误:

undefined method `photo' for #<PlacePhoto:0x007fdbbd1e75a8>

提取的源代码(第 78 行附近):

75:   <% end %>
76: 
77:   <%= f.simple_fields_for :place_photos do |photo| %>
78:     <%= photo.input :photo %>
79:     <%= photo.input :description,
80:       :label => "Photo label",
81:       :input_html => { :class => 'span4', :rows => 2 } %>

型号:

class PlacePhoto < ActiveRecord::Base
  has_attached_file :photo, 
    :styles => { :medium => "300x300#", :thumb => "100x100#" },
    :storage => :s3,
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
    :path => "/:style/:id/:filename"
  attr_accessible :description, :photo

  belongs_to :place

  validates_attachment :photo,
    :presence => true,
    :content_type => { :content_type => /image/ },
    :size => { :in => 0..2.megabytes }
end

迁移

class CreatePlacePhotos < ActiveRecord::Migration
  def change
    create_table :place_photos do |t|
      t.text :description
      t.integer :place_id
      t.has_attached_file :photo

      t.timestamps
    end
  end
end

我使用以下文章来实现 S3:http://doganberktas.com/2010/09/14/amazon-s3-and-paperclip-rails-3/。我做了捆绑安装,rake db:migrate 并重新启动了服务器。

有趣的是,当模型缺少 S3 信息时,错误并没有出现并且正在生成表单:

型号(其他版本):

class PlacePhoto < ActiveRecord::Base
  has_attached_file :photo, 
    :styles => { :medium => "300x300#", :thumb => "100x100#" }
  attr_accessible :description, :photo

  belongs_to :place

  validates_attachment :photo,
    :presence => true,
    :content_type => { :content_type => /image/ },
    :size => { :in => 0..2.megabytes }
end

【问题讨论】:

  • 我不知道 Paperclip S3 是否支持得很好,但我认为你应该填写 :bucket 选项来定位需要使用哪个桶来上传图片的回形针。
  • 桶在 config/s3.yml 中定义
  • 您也进行了迁移?请完整的错误?

标签: ruby-on-rails ruby ruby-on-rails-3 amazon-s3 paperclip


【解决方案1】:

通过改变解决的问题:

:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",

到

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 2012-08-29
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多