問題描述
帶有 S3 的回形針 ‑ 未定義的方法 (Paperclip with S3 ‑ undefined method)
I am trying to use Paperclip gem with Amazon S3 in my Rails 3.2 application. However, the problem appears when I am adding :storage => :s3 and S3 information and generating form.
Error:
undefined method `photo' for #<PlacePhoto:0x007fdbbd1e75a8>
Extracted source (around line #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 } %>
Model:
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
Migration
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
I used the folowing article to implement S3: http://doganberktas.com/2010/09/14/amazon‑s3‑and‑paperclip‑rails‑3/ . I did bundle install, rake db:migrate and restarted the server.
What is interesting, the error does not appear and the form is being generated when the model lack S3 info:
Model (other version):
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
‑‑‑‑‑
參考解法
方法 1:
Problem solved by changing:
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
To
:s3_credentials => "#{Rails.root}/config/s3.yml",