在我的Rails应用程序中,我可以让用户在创建“发行版”时上传图片,并直接上传到S3.我在开发和生产中遇到以下错误.
编辑:我应该注意,当从表单提交的发布编辑页面上传时,会发生此错误.
ArgumentError in ReleasesController#update missing required :bucket option Rails.root: /Users/jasondemeuse/pressed
我以前做过这个,没有使用Carrierwave的问题,但是无法弄清楚我在使用Paperclip时我在做错什么.我在SO和其他地方看到的所有修复都是heroku问题,但是我在开发上遇到了同样的问题,没有一个修复程序有所帮助.
development.rb
Appname::Application.configure do ... config.paperclip_defaults = { :storage => :s3,:s3_protocol => 'http',:s3_credentials => { :bucket => ENV['AWS_BUCKET'],:access_key_id => ENV['AWS_ACCESS_KEY_ID'],:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] } } end
production.rb
Appname::Application.configure do ... config.paperclip_defaults = { :storage => :s3,:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] } } end
release.rb
class Release < ActiveRecord::Base attr_accessible ... :banner belongs_to :user has_attached_file :banner,styles: { thumb: '100x100>',square: '200x200#',medium: '300x300>',spread: '1200x200' } has_many :banners,:dependent => :destroy accepts_nested_attributes_for :banners,:allow_destroy => true end
show.html.erb
<%= image_tag @release.banner.url(:medium) %> <%= @release.banner.url %> // Have both of these in for now to see if they work,but since the upload isn't working it's giving me the missing.png
_form.html.erb
<%= f.label "Add A Banner?" %><br /> <%= f.file_field :banner %>
heroku配置(在.bash_profile中有相同的开发)
AWS_ACCESS_KEY_ID: XXXXXXXXXXXXXXXX AWS_BUCKET: appname AWS_SECRET_ACCESS_KEY: XXXXXXXXXXXXXXXXXXXXXXXXXXX
编辑:这是我的控制器的相关部分
def update @release = Release.find(params[:id]) respond_to do |format| if @release.update_attributes(params[:release]) format.html { redirect_to [@user,@release],notice: 'Release was successfully updated.' } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @release.errors,status: :unprocessable_entity } end end end
我知道这应该是非常简单的,我相信我只是忘了一些明显的东西,但是我已经超过了this walkthrough以及我发现的修补程序,似乎没有任何效果.有没有耙子任务或捆绑,我忘了运行或什么?
先谢谢你!
编辑2:下面的答案帮了我很多,并切换到雾宝石固定大部分的东西给我.为了防止其他人遇到同样的问题,我也遇到了另一个问题,这让我感到困惑.如果您有英文版问题和Paperclip :: PaperclipError(缺少需要attr_accessor for’image_file_name’的项目模型),请确保您运行heroku rake db:migrate然后重新启动heroku,并重新启动.我加载了我的模式,错误地假设我不需要这样做.
上述can be found here的SO答案.
解决方法
我认为这是因为:bucket应该是一个选项传递给Paperclip而不是S3.
修复配置
修复配置
config.paperclip_defaults = { :storage => :s3,:bucket => ENV['AWS_BUCKET'],:s3_credentials => { :access_key_id => ENV['AWS_ACCESS_KEY_ID'],:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] } }
而Paperclip::Storage::S3文档似乎证实,即使写得很差/格式化.
编辑:
在我的一个项目中,我使用Paperclip与Fog宝石,这很好
Paperclip::Attachment.default_options.merge!( :storage => :fog,:fog_credentials => { :provider => 'AWS',:aws_access_key_id => ENV['S3_ACCESS_KEY_ID'],:aws_secret_access_key => ENV['S3_SECRET_ACCESS_KEY'],:region => 'eu-west-1' # in case you need it },:fog_directory => ENV['S3_BUCKET'],# only one of those is needed but I don't remember which :bucket => ENV['S3_BUCKET'] )