简体   繁体   English

Rails 错误 - 无法加载此类文件 -- aws-sdk(您可能需要安装 aws-sdk gem)

[英]Rails Error - cannot load such file -- aws-sdk (You may need to install the aws-sdk gem)

I have a RoR app with image upload through paperclip and amazon s3.我有一个 RoR 应用程序,可以通过回形针和亚马逊 s3 上传图像。 Everything was working fine until I decided to change the routes from myapp.com/id to myapp.com/model-name.一切正常,直到我决定将路由从 myapp.com/id 更改为 myapp.com/model-name。 Now I get the following error: LoadError cannot load such file -- aws-sdk (You may need to install the aws-sdk gem) .现在我收到以下错误: LoadError cannot load such file -- aws-sdk (You may need to install the aws-sdk gem) These changes involved changing the model, the controller, and the db.这些更改涉及更改 model、controller 和数据库。

Model: Model:

class Major < ActiveRecord::Base
  attr_accessible :glance, :name, :image

  # Validations
  validates :glance, presence: true
  validates :name, presence: true
  validates_attachment :image, content_type: {content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']}, size: { less_than: 5.megabytes }
  validates :slug, uniqueness: true, presence: true

  before_validation :generate_slug

  # Associations
  has_attached_file :image, styles: { 
   profile: "350x350>", 
   similar: '166x134>', 
   thumb: "100x100>" 
  },
    :storage => :s3,
    :bucket => 'major finder'
    :s3_credentials => {
      :access_key_id => 'my_key_id',
      :secret_access_key => 'my_secret_access_key'
    },
    :path => "/majors/:attachment/:style/:filename"    

  # make the url path memorable (instead of using the id)
  def to_param
    slug
  end

  def generate_slug
    self.slug ||= name.parameterize
  end 
end

Controller: Controller:

class MajorsController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :edit]
  before_filter :find_page, only: [:show, :edit, :update, :destroy]

  def index
    @majors_recent = Major.order("created_at DESC")
    @majors = Major.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @majors }
    end
  end

  def show
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @major }
    end
  end

  def new
    @major = Major.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @major }
    end
  end

  def edit
  end

  def create
    @major = Major.new(params[:major])

    respond_to do |format|
      if @major.save
        format.html { redirect_to @major, notice: 'Major was successfully created.' }
        format.json { render json: @major, status: :created, location: @major }
      else
        format.html { render action: "new" }
        format.json { render json: @major.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @major.update_attributes(params[:major])
        format.html { redirect_to @major, notice: 'Major was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @major.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @major.destroy

    respond_to do |format|
      format.html { redirect_to majors_url }
      format.json { head :no_content }
    end
  end

private 
  def find_page
    @major = Major.find_by_slug!(params[:id])
  end
end

My database schema looks like this:我的数据库架构如下所示:

  create_table "majors", :force => true do |t|
    t.text     "glance",             :limit => 255
    t.datetime "created_at",                        :null => false
    t.datetime "updated_at",                        :null => false
    t.string   "name"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

Here's my Gemfile:这是我的宝石文件:

gem 'rails', '3.2.11'
gem 'jquery-rails'
gem 'devise'
gem 'simple_form'
gem 'aws-sdk'
gem "paperclip", "~> 3.0"
gem 'sunspot_rails'
gem 'activeadmin'


group :production do
  gem 'pg'
end

group :development, :test do
  gem 'sqlite3'
  gem 'sunspot_solr'
end

group :assets do
  gem 'sass-rails', '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
  gem 'bootstrap-sass', '~> 2.2.2.0'
end

I ran $ rails g migration add_slug_to_majors slug:index and then $ rake db:migrate Now even though I've removed all the changes I am still getting this error.我运行了$ rails g migration add_slug_to_majors slug:index然后$ rake db:migrate现在即使我已经删除了所有更改我仍然收到此错误。 I have looked all over and can't find a solution.我找遍了,找不到解决办法。 Does anyone have any ideas?有人有什么想法吗?

This is actually an issue you might experience with managing versions within the Gemfile . 这实际上是您在Gemfile中管理版本时可能遇到的问题

In my case it a newer version of paperclip causing the issue . 在我的情况下,它是一个较新版本的回形针导致问题 Downgrading to and older version fixed it for me. 降级到旧版本为我修复了它。

The solution is written in error, you need aws-sdk gem also in development mode (as you're using it in your model). 该解决方案是错误编写的,您在开发模式中也需要aws-sdk gem(因为您在模型中使用它)。 Simply move gem 'aws-sdk' outside the group. 只需将gem 'aws-sdk'移到组外即可。

BTW : Use the same DB engine in dev and production mode, I have some problems with thet configuration as you have. 顺便说一句 :在开发和生产模式下使用相同的数据库引擎,我对你的配置有一些问题。

I ran into this issue today (10/5/2021) re-writing an app called FilterTRAK from Rails v4.2.1 to Rails v6.1.4.1.我今天 (10/5/2021) 在重写一个名为 FilterTRAK 的应用程序时遇到了这个问题,从 Rails v4.2.1 到 Rails v6.1.4.1。

I was getting the exact error in the subject of this question.我在这个问题的主题中遇到了确切的错误。

I was unable to downgrade paperclip due to a dependency issue on Mimemagic 0.3.0 which has been YANKED.由于对已被 YANKED 的 Mimemagic 0.3.0 的依赖性问题,我无法降级回形针。
Mimemagic 0.3.7 is all that is available and to do that you have to go up to paperclip 5.0; Mimemagic 0.3.7 是所有可用的,要做到这一点,您必须 go 到 paperclip 5.0; problem is paperclip 5.0 gave the same error.问题是回形针 5.0 给出了同样的错误。

SOLUTION:解决方案:
The gem paperclip-aws I installed that gem specifically like this: gem paperclip-aws 我专门像这样安装了那个 gem:

gem 'paperclip-aws', '~> 1.6', '>= 1.6.8'

And my application loaded up correctly.我的应用程序加载正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM