【发布时间】:2013-05-08 09:59:20
【问题描述】:
我正在使用 Paperclip 上传多张图片并将其存储在 s3 中。 所以,我有一个画廊模型,看起来像这样:
class Gallery < ActiveRecord::Base
attr_accessible :title, :body, :pictures_attributes
has_many :pictures
accepts_nested_attributes_for :pictures, :allow_destroy => true
end
和画廊应该有很多图片。我的图片模型如下所示:
class Picture < ActiveRecord::Base
belongs_to :gallery
has_attached_file :picture, :styles => { :small => "150x150>", :medium => "300x300" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/amazon_s3.yml",
:path => "/:class/:style/:id/:filename"
validates_attachment_presence :picture
validates_attachment_size :picture, :less_than => 5.megabytes
validates_attachment_content_type :picture, :content_type => ['image/jpeg', 'image/png']
end
我已经把它放在了我的 _form.html.erb 中:
<%= form_for @gallery, :html => { :multipart => true } do |f| %>
还有这个
<%= f.fields_for :picture do |picture_form| %>
<p>
<%= picture_form.file_field :picture %>
</p>
<% end %>
在我的画廊控制器中,我有这个:
def new
@gallery = Gallery.new
5.times{ @gallery.pictures.build }
end
# GET /galleries/1/edit
def edit
@gallery = Gallery.find(params[:id])
5.times{ @gallery.pictures.build }
end
# POST /galleries
# POST /galleries.xml
def create
@gallery = Gallery.new(params[:gallery])
respond_to do |format|
if @gallery.save
format.html { redirect_to(admin_gallery_path(@gallery), :notice => 'Gallery was successfully created.') }
format.xml { render :xml => @gallery, :status => :created, :location => @gallery }
else
format.html { render :action => "new" }
format.xml { render :xml => @gallery.errors, :status => :unprocessable_entity }
end
end
end
我发现了一些类似的案例,按照答案。但我仍然收到相同的错误消息。 我试图将 RAILS_ROOT 更改为 Rails.root,但没有帮助。 我尝试关注this 的回答,但我不确定在哪里将参数传递给回形针?
谁知道问题出在哪里?谢谢
【问题讨论】:
-
只是我在您的回形针配置中注意到的...您的存储桶在哪里?您实际上在哪里保存图像?您需要在您的 aws 帐户中创建一个存储桶
-
你的意思是::storage => :s3, :s3_credentials => "#{Rails.root}/config/amazon_s3.yml" 我不知道你说的桶是什么意思。我很抱歉。但我在其他模型(不是nested_form)中使用了回形针和s3,它可以工作
-
是什么意思?认为你错过了什么
-
是的,抱歉,刚刚编辑过
标签: ruby-on-rails-3 amazon-s3 paperclip nested-forms