Ruby-on-rails NameError(未初始化的常量 Paperclip::Storage::S3::AWS):
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28374401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NameError (uninitialized constant Paperclip::Storage::S3::AWS):
提问by EggSix
I'm trying to incorporate images into my web app and I keep running into this error after removing quite a few features. It came down to my 'create' application controller and I'm not entirely sure where I should go from here.
我正在尝试将图像合并到我的 Web 应用程序中,但在删除了相当多的功能后,我一直遇到这个错误。它归结为我的“创建”应用程序控制器,我不完全确定我应该从哪里开始。
2015-02-06T20:30:12.292187+00:00 app[web.1]: (1.9ms) ROLLBACK
2015-02-06T20:30:12.296299+00:00 app[web.1]: NameError (uninitialized constant Paperclip::Storage::S3::AWS):
2015-02-06T20:30:12.296301+00:00 app[web.1]: app/controllers/articles_controller.rb:24:in `create'
2015-02-06T20:45:14.691084+00:00 app[web.1]: [paperclip] saving /articles/images/000/000/013/original/git.jpeg
2015-02-06T20:45:14.698744+00:00 app[web.1]: Completed 500 Internal Server Error in 584ms
2015-02-06T20:45:14.700871+00:00 heroku[router]: at=info method=POST path="/articles" host=preston.herokuapp.com request_id=d9d02257-3616-4686-bce5-3d912cd528c2 fwd="76.22.102.38" dyno=web.1 connect=1ms service=698ms status=500 bytes=1754
Articles_controller.rb
Articles_controller.rb
class ArticlesController < ApplicationController
http_basic_authenticate_with name: "name", password: "password", except: [:index, :show]
def index
@articles = Article.all.order("created_at DESC")
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text, :image)
end
end
Gemfile
文件
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.2.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'bootstrap-sass', '~> 3.3.3'
gem 'autoprefixer-rails'
gem 'paperclip', '~> 4.2.1'
gem 'aws-sdk', '~> 2.0.22'
group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :doc do
gem 'sdoc', '~> 0.4.0', require: false
end
回答by TopaZ
Modify your Gemfile's aws-sdk to install a version prior to 2.0:
修改 Gemfile 的 aws-sdk 以安装 2.0 之前的版本:
gem 'aws-sdk', '< 2.0'
This issue was introduced with new version of aws-sdk (2.0+). You can read more here: http://ruby.awsblog.com/post/TxFKSK2QJE6RPZ/Upcoming-Stable-Release-of-AWS-SDK-for-Ruby-Version-2
这个问题是在新版本的 aws-sdk (2.0+) 中引入的。您可以在此处阅读更多信息:http: //ruby.awsblog.com/post/TxFKSK2QJE6RPZ/Upcoming-Stable-Release-of-AWS-SDK-for-Ruby-Version-2
回答by Vitali Mogilevsky
There is official solution Use paperclip from this branch: it works with aws-sdk versions above 2
有官方解决方案 Use paperclip from this branch: it works with aws-sdk version above 2
gem 'paperclip', :git=> 'https://github.com/thoughtbot/paperclip', :ref => '523bd46c768226893f23889079a7aa9c73b57d68'
just add :s3_region parameter to your paperclip s3 config
只需将 :s3_region 参数添加到您的回形针 s3 配置中
works for me
对我来说有效
回答by RichiRich
I got it to work by navigating to my gem folder and changing the Gems to:
我通过导航到我的 gem 文件夹并将 Gems 更改为:
- gem ‘paperclip'
- gem ‘aws-sdk'
- 宝石“回形针”
- 宝石'aws-sdk'
The version declarations can be dropped.
可以删除版本声明。
To avoid getting a gem.lock error, run bundle updateinstead of bundle install, otherwise only the gems will be updated.
为避免获得gem.lock error,请运行bundle update而不是bundle install,否则只会更新宝石。
Now, the heroku logs -tcommand can be used to monitor the heroku server to image uploads.
现在,该heroku logs -t命令可用于监控 heroku 服务器以上传图像。
I orginally received a new error, Access Denied Errorfor AWS server.
我最初收到一个新的错误,Access Denied ErrorAWS 服务器。
To fix this I found the Active Access Key IDwith the latest date on the Amazon websiteand used heroku commands to input the latest Access key IDand Secret access key.
为了解决这个问题,我Active Access Key ID在亚马逊网站上找到了最新的日期并使用 heroku 命令输入最新的Access key ID和Secret access key.
This enabled me to view my image on heroku.
这使我能够在 heroku 上查看我的图像。
I had made so many Access key IDand Secret access keystrying to fix the problem, but found the Gems to be the real problem.
我做了这么多Access key ID并Secret access keys试图解决问题,但发现宝石才是真正的问题。
Tip: Save all your Access Key info to OneNote, Notepad, etc. This way you can return and check them.
提示:将您所有的访问密钥信息保存到 OneNote、记事本等。这样您就可以返回并检查它们。
回答by equivalent8
Paperclip use to use AWS-SDK v1 in versions 4.3 and bellow. They are trying to include the AWS-SDK v2
Paperclip 用于在 4.3 及以下版本中使用 AWS-SDK v1。他们正在尝试包含 AWS-SDK v2
official upgrade document https://github.com/thoughtbot/paperclip/blob/master/UPGRADING
官方升级文档https://github.com/thoughtbot/paperclip/blob/master/UPGRADING
##################################################
# NOTE FOR UPGRADING FROM 4.3.0 OR EARLIER #
##################################################
Paperclip is now compatible with aws-sdk >= 2.0.0.
If you are using S3 storage, aws-sdk >= 2.0.0 requires you to make a few small
changes:
* You must set the `s3_region`
* If you are explicitly setting permissions anywhere, such as in an initializer,
note that the format of the permissions changed from using an underscore to
using a hyphen. For example, `:public_read` needs to be changed to
`public-read`.
due to some backwards incomparability (read this https://github.com/thoughtbot/paperclip/issues/2021) this is merged but officially not released yet, but should be released in Paperclip v 5.0.0
由于一些向后的不可比性(阅读这个https://github.com/thoughtbot/paperclip/issues/2021)这是合并但尚未正式发布,但应该在 Paperclip v 中发布5.0.0
So like Vitali Mogilevskymentioned, you have to use this for now:
所以就像Vitali Mogilevsky提到的那样,你现在必须使用它:
# Gemfile
# ...
gem 'paperclip', :git=> 'https://github.com/thoughtbot/paperclip', :ref => '523bd46c768226893f23889079a7aa9c73b57d68'
When Paperclip 5.0 is released, AWS-SDK v2 should be included
当 Paperclip 5.0 发布时,应包含 AWS-SDK v2

