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

时间:2013-02-25 23:21:36

标签: ruby-on-rails ruby-on-rails-3 amazon-s3 paperclip

我有一个RoR应用程序,通过回形针和亚马逊s3上传图片。在我决定将路线从myapp.com/id更改为myapp.com/model-name之前,一切正常。现在我收到以下错误:LoadError cannot load such file -- aws-sdk (You may need to install the aws-sdk gem)。这些更改涉及更改模型,控制器和数据库。

型号:

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

控制器:

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

我的数据库架构如下所示:

  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

这是我的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

我跑$ rails g migration add_slug_to_majors slug:index然后$ rake db:migrate 现在,即使我已删除所有更改,我仍然会收到此错误。我看了一遍,找不到解决方案。有没有人有任何想法?

2 个答案:

答案 0 :(得分:4)

这实际上是您在 Gemfile 中管理版本时可能遇到的问题

就我而言,它是 paperclip 的新版本,导致问题。降级到旧版本为我修复了它。

答案 1 :(得分:3)

解决方案是错误编写的,您在开发模式下也需要aws-sdk gem(因为您在模型中使用它)。只需将gem 'aws-sdk'移到小组之外。

BTW :在开发和生产模式下使用相同的数据库引擎,我在配置方面遇到了一些问题。