名称错误(未初始化常量Paperclip::Storage::S3::AWS)

91

我正在尝试将图像整合到我的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

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

是的,我已经找到了,并且正在查看Heroku日志以找到该错误。 - EggSix
3
很可能是因为您没有在Heroku中初始化AWS常量,您需要运行以下命令: $ heroku config:set S3_BUCKET_NAME=您的桶名称 $ heroku config:set AWS_ACCESS_KEY_ID=您的访问密钥ID $ heroku config:set AWS_SECRET_ACCESS_KEY=您的秘密访问密钥 - Ahmad Al-kheat
好的,请告诉我它是否有效,这样我就可以将其作为答案供其他人受益。 - Ahmad Al-kheat
我仍然收到之前相同的错误。 - EggSix
这对我解决了问题。 - superluminary
显示剩余2条评论
4个回答

179

2
我发现问题出在更新的'aws-sdk' gem上。有一个新版本(2+)的aws-sdk,它与旧版本不兼容。您可以在这里阅读更多信息:(https://github.com/aws/aws-sdk-ruby#nameerror-uninitialized-constant-aws) - TopaZ
谢谢,正是我所需要的! - Sprachprofi
27
另外,您可以使用 gem 'aws-sdk-v1' 替换该行。这将使您能够引入 v2 aws-sdk gem。因为具有不同的命名空间,所以它们可以在同一个应用程序中一起使用。 - Trevor Rowe
Trevor Rowe的解决方案对我非常有效--而且它们可以同时使用,这非常有用。谢谢Trevor! - XtraSimplicity

18

有官方解决方案:使用此分支中的纸夹(paperclip):它与2以上版本的aws-sdk兼容。

gem 'paperclip', :git=> 'https://github.com/thoughtbot/paperclip', :ref => '523bd46c768226893f23889079a7aa9c73b57d68'

只需在您的Paperclip S3配置中添加:s3_region参数即可。

对我有效。


1
这是目前最好的答案,因为aws 1已经被弃用。 - port5432

4
我通过进入我的gem文件夹并更改Gems来使它工作:
  • gem ‘paperclip’
  • gem ‘aws-sdk’
版本声明可以省略。
为了避免出现“gem.lock错误”,请运行bundle update而不是bundle install,否则只会更新gems。
现在,可以使用heroku logs -t命令监视heroku服务器的图像上传。
我最初收到一个新的错误,AWS服务器的“Access Denied Error”。
要修复此问题,我在Amazon网站上找到了最新日期的“Active Access Key ID”,并使用heroku命令输入了最新的“Access key ID”和“Secret access key”。
这使我能够在heroku上查看我的图像。
我制作了许多“Access key ID”和“Secret access keys”,试图解决问题,但发现Gems才是真正的问题。
提示:将所有Access Key信息保存到OneNote、记事本等中。这样您就可以返回并检查它们。

我遇到了相同的问题,你通过删除版本解决了吗? - Nomad

3
Paperclip曾在4.3及以下版本中使用AWS-SDK v1。他们正在尝试包含AWS-SDK v2。
官方升级文档 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`.

由于一些向后不兼容的问题(请阅读https://github.com/thoughtbot/paperclip/issues/2021),这已经合并,但官方尚未发布,但应该在Paperclip v 5.0.0中发布。
所以像Vitali Mogilevsky提到的那样,你现在必须使用这个。
# Gemfile
# ...
gem 'paperclip', :git=> 'https://github.com/thoughtbot/paperclip', :ref => '523bd46c768226893f23889079a7aa9c73b57d68'

当Paperclip 5.0发布时,应该包含AWS-SDK v2。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接